PAT 1133 Splitting A Linked List[链表][简单]
1133 Splitting A Linked List(25 分)
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤103). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer in [−105,105], and Next
is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
Sample Output:
33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1
题目大意:给出一个链表,和一个数K,将<0的数都按发现的顺序放在开头,<=k的数放在负数后,并且也是按原来的顺序,>k的按顺序在最后。
//很简答的题目,一开始遍历的方式错了,不过改了过来,
AC代码:
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
struct Node{
int data,next;
}vn[]; int main() {
int from,n,k;
cin>>from>>n>>k;
// vector<Node> vn(n);
int add,da,to;
for(int i=;i<n;i++){
cin>>add>>da>>to;
//vn[add].addr=add;
vn[add].data=da;
vn[add].next=to;
}
vector<int> re;
//本次找出是负数的,并且编号存储。
// for(int i=0;i<n;i++){
// if(vn[i].data<0)
// re.push_back(i);
// }
// for(int i=0;i<n;i++){
// if(vn[i].data>=0&&vn[i].data<=k)
// re.push_back(i);
// }
// for(int i=0;i<n;i++){
// if(vn[i].data>k)
// re.push_back(i);
// }这样去遍历链表是不对的,并不能分出来次序啊。
for(int i=from;i!=-;i=vn[i].next){
if(vn[i].data<)
re.push_back(i);
}
for(int i=from;i!=-;i=vn[i].next){
if(vn[i].data>=&&vn[i].data<=k){
re.push_back(i);
}
}
for(int i=from;i!=-;i=vn[i].next){
if(vn[i].data>k)
re.push_back(i);
}
for(int i=;i<re.size();i++){
if(i==re.size()-){
printf("%05d %d -1",re[i],vn[re[i]].data);
}//cout<<re[i]<<" "<<vn[re[i]].data<<"-1";
else{
printf("%05d %d %05d\n",re[i],vn[re[i]].data,re[i+]);
}//cout<<re[i]<<" "<<vn[re[i]].data<<" "<<re[i+1]<<'\n';
} return ;
}
1.链表遍历的主要就是使用数组下标表示链表地址,根据这个对链表进行遍历,其他的问题就不大了。
2。从前往后遍历链表3次,按顺序放入地址,之后再按顺序输出即可!
PAT 1133 Splitting A Linked List[链表][简单]的更多相关文章
- PAT 1133 Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT A1133 Splitting A Linked List (25 分)——链表
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...
- PAT1133:Splitting A Linked List
1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT_A1133#Splitting A Linked List
Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...
- PAT-1133(Splitting A Linked List)vector的应用+链表+思维
Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
随机推荐
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法 LBS 球面距离公式 http://wiki.myoa.info/zh-blog:20 Java,Mysql- ...
- php 显示一个干净的,易被解析的json
header("Content-type: text/html; charset=utf-8"); //试着从数据库里读取一条数据放进来 $con = mysql_connect( ...
- Java的图形界面依然是跨平台的
Awt:抽象窗口工具箱,它由三部分组成: ①组件:界面元素: ②容器:装载组件的容器(例如窗体): ③布局管理器:负责决定容器中组件的摆放位置. 图形界面的应用分四步: ① 选择一个容器: ⑴wind ...
- remote指令添加远程数据库
git remote add <name> <url> git remote add origin https://[your_space_id].backlogtool.co ...
- MFC获取系统当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是%Y年% ...
- 一道money计算题引发的思考
网友提出一个问题如下 是小学和中学时候学到了增长折线问题,有点像数学问题,不过这个要求用编程来实现,恐怕还是有些逻辑要处理的,话不多说看代码吧 我给出的代码如下 代码清单: <?php func ...
- 自定义View总结
写的很好,代你分析原码,关于 View Measure 测量机制,让我一次把话说完
- STM32 PWM的输出与Keil软件仿真
导读:PWM(Pulse Width Modulation)控制——脉冲宽度调制技术,通过对一系列脉冲的宽度进行调制,来等效地获得所需要波形(含形状和幅值). PWM控制技术在逆变电路中应用最广,应用 ...
- post 与get
GET:从服务器上获取数据,也就是所谓的查,仅仅是获取服务器资源,不进行修改. POST:向服务器提交数据,这就涉及到了数据的更新,也就是更改服务器的数据. 补充: PUT:PUT的英文含义是放置,也 ...
- java中的最重要的 集合框架
java.util这个重要的包包含大量的类和接口,支持很多的功能.例如,java.util具有能产生伪随机数的类,还包括可以管理日期和时间.观察事件.操作位集合.标记字符串.处理格式化数据等的类.ja ...