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 ...
随机推荐
- leetcode-158周赛-5224-掷筛子模拟
题目描述: 方法:动态规划O(6∗6∗n∗15) 递归: from functools import lru_cache class Solution: def dieSimulator(self, ...
- css清除浮动的几种方法
推荐几种好用的清除浮动方法: 方法1: .clearfix:after { content:"."; display:block; height:; clear:both; vis ...
- NX二次开发-UFUN CSYS坐标系转换UF_CSYS_map_point
1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_curve.h> 5 #include <uf_csys.h> 6 ...
- NX二次开发-UFUN所有对象类型的宏定义
/**************************************************************************** Copyright (c) 2010 Sie ...
- vue中使用router全局守卫实现页面拦截
一.背景 在vue项目中使用vue-router做页面跳转时,路由的方式有两种,一种是静态路由,另一种是动态路由.而要实现对路由的控制需要使用vuex和router全局守卫进行判断拦截(安全问题文章最 ...
- POJ2186-Tarjan-kosaraju-缩点
目录 目录 思路: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门 原题目描述在最下面. A认为B优秀,B认为C优秀,则A认为C优秀.问有多少个人被其他所有人 ...
- 快速排序--Python实现
快速排序算法:1.选择一个基准数2.小于基准数的放左边,大于基准数的放右边3.利用递归的方法针对左边的数据进行快速排序,再对右边的数据进行快速排序4.递归停止的条件:数组为空或者只有一个元素 时间复杂 ...
- LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)
题目标签:Binary Search 题目给了我们一组 int array,让我们找到数组的 peak. 利用 binary search, 如果数字比它后面那个数字小,说明还在上坡,缩小范围到右半边 ...
- 拾遗:Perl 基础语法
Perl 常用的命令行参数 -i:将处理结果直接写入文件,可以通过 -i.bak 或 -i"/tmp/orig_*" 等形式,在修改之前进行备份 -e:启用 perl 的命令行模式 ...
- C#中ORM的简单实现
ORM在功能上主要有两个: 把从数据库中查询返回的DataSet,DataTable转化为我们可以方便使用的实体类集合: 把要对数据库操作的实体类集合或条件转化为数据库可以直接执行的SQL语句.