PAT (Basic Level) Practise (中文)1075. 链表元素分类(25)
来源:互联网 发布:淘宝大件物流怎么设置 编辑:程序博客网 时间:2024/06/09 15:14
1075. 链表元素分类(25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而[0, K]区间内的元素都排在大于K的元素前面。但每一类内部元素的顺序是不能改变的。例如:给定链表为 18→7→-4→0→5→-6→10→11→-2,K为10,则输出应该为 -4→-6→-2→7→0→5→10→18→11。
输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出:第1个结点的地址;结点总个数,即正整数N (<= 105);以及正整数K (<=1000)。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址;Data是该结点保存的数据,为[-105, 105]区间内的整数;Next是下一结点的地址。题目保证给出的链表不为空。
输出格式:
对每个测试用例,按链表从头到尾的顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。
输入样例:00100 9 1023333 10 2777700000 0 9999900100 18 1230968237 -6 2333333218 -4 0000048652 -2 -199999 5 6823727777 11 4865212309 7 33218输出样例:
33218 -4 6823768237 -6 4865248652 -2 1230912309 7 0000000000 0 9999999999 5 2333323333 10 0010000100 18 2777727777 11 -1
#include <cstdio>#include <vector>using namespace std;const int maxn=1e5+10;struct Node{ int data,next;}node[maxn];vector<int> v[3];int main(){ int start,n,k,a; scanf("%d%d%d",&start,&n,&k); for(int i=0;i<n;i++){ scanf("%d",&a); scanf("%d%d",&node[a].data,&node[a].next); } int p=start; while(p!=-1){ int data=node[p].data; if(data<0) v[0].push_back(p); else if(data>=0&&data<=k) v[1].push_back(p); else v[2].push_back(p); p=node[p].next; } int flag=0; for(int i=0;i<3;i++){ for(int j=0;j<v[i].size();j++){ if(!flag){ printf("%05d %d ",v[i][j],node[v[i][j]].data); flag=1; } else printf("%05d\n%05d %d ",v[i][j],v[i][j],node[v[i][j]].data); } } printf("-1"); return 0;}
阅读全文
0 0
- PAT (Basic Level) Practise (中文)1075. 链表元素分类(25)
- PAT (Basic Level) Practise (中文) 1075. 链表元素分类(25)
- PAT (Basic Level) Practise (中文)1012. 数字分类 (20)
- PAT (Basic Level) Practise (中文)1012. 数字分类 (20)
- PAT (Basic Level) Practise (中文) 1012. 数字分类 (20)
- PAT (Basic Level) Practise (中文)1012. 数字分类 (20)
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)C语言
- PAT (Basic Level) Practise (中文) 1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)--1001
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)1001
- PAT (Basic Level) Practise (中文)1002
- PAT (Basic Level) Practise (中文) 1007
- PAT (Basic Level) Practise (中文)1015. 德才论 (25)
- java连接不上sq server 2012 express的解决方法
- ES6的变量
- Kivy显示中文
- 欧几里得+扩展欧几里得(理解)
- Android MVP 架构及布局业务逻辑复用
- PAT (Basic Level) Practise (中文)1075. 链表元素分类(25)
- 自定义的动态链接库使用
- C++ 未知数量参数/list的未知数量的初始化
- 计算机中的颜色表示法——RGB
- 2017 北京区域赛 J题
- char * 指针
- CMake封装库
- (转)Java RMI远程方法调用详解
- c语言缓冲区