PAT1133: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 (<=1000). 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 思路 逻辑水题,将一个链表划分成三个区间。 代码
#include<iostream>
#include<vector>
using namespace std;
class node
{
public:
int val;
int next;
};
vector<node> nodes(100000);
int main()
{
vector<vector<int>> res(3);
int start,N,K;
while(cin >> start >> N >> K)
{
for(int i = 0;i < N;i++)
{
int tmp;
cin >> tmp;
cin >> nodes[tmp].val >> nodes[tmp].next;
}
while(start != -1)
{
if(nodes[start].val < 0)
res[0].push_back(start);
else if(nodes[start].val >= 0 && nodes[start].val <= K)
res[1].push_back(start);
else
res[2].push_back(start);
start = nodes[start].next;
}
int cnt = 0;
for(int i = 0;i < res.size();i++)
{
for(int j = 0;j < res[i].size();j++)
{
if(cnt++ == 0)
{
printf("%05d %d ",res[i][j],nodes[res[i][j]].val);
}
else
{
printf("%05d\n%05d %d ",res[i][j],res[i][j],nodes[res[i][j]].val);
}
}
}
printf("-1");
}
}
PAT1133:Splitting A Linked List的更多相关文章
- PAT-1133 Splitting A Linked List(链表分解)
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long int ...
- PAT-1133(Splitting A Linked List)vector的应用+链表+思维
Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...
- PAT 1133 Splitting A Linked List[链表][简单]
1133 Splitting A Linked List(25 分) Given a singly linked list, you are supposed to rearrange its ele ...
- PAT_A1133#Splitting A Linked List
Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...
- A1133. 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 ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...
- 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 ...
随机推荐
- 重装Windows后修复Linux引导
装了双系统(Windows和Linux)的机器重新安装Windows后会导致Linux的引导丢失而无法进入原先的Linux系统[其原因是Windows会覆盖原先MBR中的Linux的BootLoade ...
- 生产者消费者的java实现
先看最简单的,也就是缓冲区的容量为1 缓冲区容量为1 import java.util.List; public class ProducerAndConsumer2 { static class A ...
- netstat 的10个基本用法(转)
本文转载自一译作. *注:netstat即network state缩写. Netstat 简介 Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以 ...
- vi/vim下看十六进制文件
:%!xxd --将当前文本转换为16进制格式. 查看内容是对应的.你可以后面看到对应的字符内容 :%!od --将当前文本转换为16进制格式. :%!xxd -c 12--将当前文本转换为16进制格 ...
- DB 查询分析器 6.03 ,遨游于任何Windows操作系统之上的最优秀的数据库客户端工具
DB 查询分析器 6.03 ,遨游于任何Windows操作系统之上的最优秀的数据库客户端工具 中国本土程序员马根峰(CSDN专访马根峰:海量数据处理与分析大师的中国本土程序员 .03版本已经完全兼容 ...
- SQLite数据库中多线程使用问题
由于项目是接手之前的烂尾项目,经常被吐槽说界面卡半天,后来发现项目里的网络请求,数据库操作都是在主线程.将一些长时间的操作换到多线程或者异步之后后,用户交互是变的顺畅多了,可是经常出现莫名其妙的闪退, ...
- Leetcode_260_Single Number III
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/50276549 Given an array of numb ...
- java获取时间
string startTimeStr = ((String) jsonCampaign.get(configObj.getKeyword(config.START_TIME))); ...
- 简单了解AJAX
// ajax的俩个版本: var xmlhttp; if(window.xmlHttpRequest){ xmlhttp = new xmlHttpRequest(); }else{ xmlhttp ...
- oracle面试题目总结
阿里巴巴公司DBA笔试题 http://searchdatabase.techtarget.com.cn/tips/2/2535002.shtml 注:以下题目,可根据自己情况挑选题目作答,不必 ...