PAT甲级——A1133 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 (≤) which is the total number of nodes, and a positive K (≤). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.
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 [, 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
新建3条链表,分别存负数,[0,k]的数以及大于k的数。然后将三条链表窜起来
当然,更省事的,就直接使用3个queue分别记录三组数据,然后输出,
两个方法差不多,容器的更方便,但链表的不用额外空间
#include <iostream>
#include <vector>
using namespace std;
struct Node
{
int val, next;
}List[];
int head, n, k;
int main()
{
cin >> head >> n >> k;
while (n--)
{
int address, data, next;
cin >> address >> data >> next;
List[address].val = data;
List[address].next = next;
}
int head1 = , head2 = , head3 = ;//分别是负数、中间数、>k数的链表
int p = head, p1 = head1, p2 = head2, p3 = head3;
while (p != -)
{
if (List[p].val < )
{
List[p1].next = p;
p1 = p;
}
else if (List[p].val > k)
{
List[p3].next = p;
p3 = p;
}
else
{
List[p2].next = p;
p2 = p;
}
p = List[p].next;
}
//这里的顺序千万不要反了,因为next不是地址,要先改变,再赋值
List[p3].next = -;
List[p2].next = List[head3].next;
List[p1].next = List[head2].next;
p = List[head1].next;
while (List[p].next != -)
{
printf("%05d %d %05d\n", p, List[p].val, List[p].next);
p = List[p].next;
}
printf("%05d %d %d\n", p, List[p].val, List[p].next);
return ;
}
PAT甲级——A1133 Splitting A Linked List【25】的更多相关文章
- 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 ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT甲级:1036 Boys vs Girls (25分)
PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 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 ...
- A1133. Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT甲级 1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
随机推荐
- 使用 Google Chrome 数据抓包方式免费下载收费音乐
对于每个有音乐细胞的 boys & girls 来说,听音乐的时候是不是会经常遇到如下图的问题. 自从音乐进入正版之后,很多歌曲只有付费用户才可以下载.虽然可以在线听,可是以我的倔脾气,就是喜 ...
- 【Nginx】Nginx配置
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...
- 2019.7.3模拟 七星连珠(行列式+随机+k进制FWT)
题目大意: 给一个\(n*n\)的矩阵,对于所有排列p,记录\(a[i][p[i]]\)的k进制下不进位加法的结果,问所有被记录过的数. \(n<=50,p=2.3,0<=a[i][j]& ...
- Struts2入门的第一个应用
今天开始学习struts2技术,现在struts2的技术已经超过了struts1,所以本人就没有学习struts1了,当然这个肯定不会影响我们后面的学习,先来看一下工程的目录结构: 说明: query ...
- NOIp2018集训test-9-17(am)
这是一套去年在长沙考过的题,但是我当时就没理清楚+没写题解(我以前很多博客都写得跟shi一样,完全没有意义,看到就想打当时的我),所以又考得稀烂. T1.star way to heaven 容易想到 ...
- NX二次开发-UFUN创建基准平面UF_MODL_create_plane
NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建基准平面 ] = {0.0, 0.0, ...
- Jboss集群(五)--F5硬件负载均衡器双击热备 + Jboss集群终极实现
BIG/IP利用定义在其上面的虚拟IP地址来为用户的一个或多个应用服务器提供服务.因此,它能够为大量的基于TCP/IP的网络应用提供服务器负载均衡服务.BIG/IP连续地对目标服务器进行L4到L7合理 ...
- C/Python/Java环境变量配置
链接 全图预览: Java: 只需添加下面三个环境变量即可使用Java.对照这我的添加就行. CLASSPATH的内容: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\li ...
- 笔试题-求小于等于N的数中有多少组素勾股数
题目描述: 一组勾股数满足:a2+b2=c2: 素勾股数:a,b,c彼此互质. 输入正整数N: 输出小于等于N的数中有多少组勾股数. 例: 输入:10 输出:1 思路:我是直接暴力破解的…… 首先找出 ...
- idea中选中了一个变量名,会高亮显示位于别的地方的这个变量名,那么怎么修改其他地方的高亮颜色