1052. Linked List Sorting (25)
2015-12-06 11:05
381 查看
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:[/b]
来源: <http://www.patest.cn/contests/pat-a-practise/1052>
[/code]
来自为知笔记(Wiz)
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:[/b]
5 00001 11111 100 -1 00001 0 22222 33333 100000 11111 12345 -1 33333 22222 1000 12345Sample Output:[/b]
5 12345 12345 -1 00001 00001 0 11111 11111 100 22222 22222 1000 33333 33333 100000 -1
来源: <http://www.patest.cn/contests/pat-a-practise/1052>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#pragma warning(disable:4996)
using namespace std;
struct List
{
int address;
int value;
//int next;
};
vector<List> l;
bool cmp(List a, List b) {
return a.value < b.value;
}
int val[100010] = { 0 }, nex[100010] = { 0 };
int main(void) {
for (int i = 0; i < 100010; i++) {
val[i] = 100010;
nex[i] = 100010;
}
int n, start_addr;
cin >> n >> start_addr;
if (start_addr == -1) {
cout << "0 -1" << endl;
return 0;
}
int addr_temp, val_temp, next_temp;
for (int i = 0; i < n; i++) {
scanf("%d", &addr_temp);
scanf("%d", &val[addr_temp]);
scanf("%d", &nex[addr_temp]);
}
int addr = start_addr;
List list_temp;
while (true)
{
list_temp.address = addr;
list_temp.value = val[addr];
//list_temp.next = nex[addr];
l.push_back(list_temp);
addr = nex[addr];
if (addr == -1||nex[addr]>100001)
break;
}
sort(l.begin(), l.end(), cmp);
printf("%d %05d\n", l.size(), l[0].address);
for (int i = 0; i < l.size(); i++) {
if (i != l.size() - 1)
printf("%05d %d %05d\n", l[i].address, l[i].value, l[i + 1].address);
else
printf("%05d %d -1\n", l[i].address, l[i].value);
}
return 0;
}
[/code]
来自为知笔记(Wiz)
相关文章推荐
- mac上对CentOS6.5安装VNC流程
- ThinkPHP框架八验证码和用户的验证
- 原码, 反码, 补码 详解
- 抽象类与接口的区别
- 【Leetcode】Contains Duplicate II
- 【C语言提高28】多级指针
- 我与Github
- Mac之Git/GitHub使用(2)——Create A Repo
- 【linux】进程间的通信简介(c语言编程)
- 如何去理解马克思主义和哲学的含义
- TranslateMessage和DispatchMessage作用
- php的大小写敏感问题整理
- 弹出textview的同时弹出输入法软键盘
- 1051. Pop Sequence (25)
- 九度oj-1362-左旋转字符串
- 【C语言提高27】二级指针三种内存模型总述
- [MI-BCI论文札记]基于Morse电码的多类运动想象BCI系统设
- 信息安全系统设计基础第十二周学习总结—20135227黄晓妍
- JavaWeb学习(5.5)---在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 1050. String Subtraction (20)