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 ...
随机推荐
- 因此mybatis最好与spring集成起来使用
单独使用mybatis是有很多限制的(比如无法实现跨越多个session的事务),而且很多业务系统本来就是使用spring来管理的事务,因此mybatis最好与spring集成起来使用. spring ...
- SSM,即Spring+SpringMVC+MyBatis三个开源框架的整合框架集。
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...
- 程序员自己编写的类和JDK类是一种合作关系
封装类: JAVA为每一个简单数据类型提供了一个封装类,使每个简单数据类型可以被Object来装载. 除了int和char,其余类型首字母大写即成封装类. 转换字符的方式: int I=10; Str ...
- 解决 Failure to transfer * from http://repo1.maven.org/maven2
解决 Failure to transfer * from http://repo1.maven.org/maven2 Failure to transfer org.apache.maven:mav ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- BEGIN_MESSAGE_MAP(Caccess_test_1Dlg, CDialogEx)
BEGIN_MESSAGE_MAP(...消息映射宏的一部分.ON_WM_CREATE()产生一个消息处理函数映射项目,把WM_CREATE和OnCreate函数联系起来. 参数的个数和类型是系统已经 ...
- (转)python中的参数:*args和**kwargs
def foo(*args, **kwargs):print 'args = ', argsprint 'kwargs = ', kwargsprint '---------------------- ...
- 复习及总结--.Net线程篇(3)
不幸的发现,原来多线程的东西还有好多. 不只是一个Thread就能把事情做完的,好吧,孤陋寡闻了 这里总结下 复习及总结--.Net线程篇(2)里的两个概念AppDomain和ThreadPool ...
- linux 命令集合收集(ubuntu)
1.查看ubuntu版本号:cat /etc/issue 或者sudo lsb_release -a 2.查看内核版本信息:uname -a ,显示为4.4.0 x86_64版本内核
- 如何在 Linux 上录制你的终端操作
导读 录制一个终端操作可能是一个帮助他人学习 Linux .展示一系列正确命令行操作的和分享知识的通俗易懂方法.不管是出于什么目的,从终端复制粘贴文本需要重复很多次,而录制视频的过程也是相当麻烦,有时 ...