pat 1052

2024-02-11 16:48
文章标签 pat 1052

本文主要是介绍pat 1052,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard

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:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
 
#include <stdio.h>
#include <algorithm>
using namespace std;struct Node
{int pre;int data;int next;
} node[100010], nn[100010];bool cmp(Node a, Node b)
{return a.data < b.data;
}int main()
{int n, head, pre, data, next;int i, j;scanf("%d%d", &n, &head);if(head == -1){//如果链表为空,输出0,-1printf("0 -1\n");return 0;}for(i = 0; i < n; i++){scanf("%d%d%d", &pre, &data, &next);node[pre].pre = pre;node[pre].data = data;node[pre].next = next;}//将不是链表中的结点删除j = head;i = 0;while(j != -1){nn[i].pre = node[j].pre;nn[i].data = node[j].data;nn[i].next = node[j].next;j = node[j].next;i++;}n = i;sort(nn, nn + i, cmp);head = nn[0].pre;printf("%d %05d\n", n, head);for(i = 0; i < n; i++){if(i != n-1)printf("%05d %d %05d\n", nn[i].pre, nn[i].data, nn[i+1].pre);elseprintf("%05d %d -1\n", nn[i].pre, nn[i].data);}return 0;
}


这篇关于pat 1052的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/700307

相关文章

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

PAT (Advanced Level) Practice——1011,1012

1011:  链接: 1011 World Cup Betting - PAT (Advanced Level) Practice (pintia.cn) 题意及解题思路: 简单来说就是给你3行数字,每一行都是按照W,T,L的顺序给出相应的赔率。我们需要找到每一行的W,T,L当中最大的一个数,累乘的结果再乘以0.65,按照例子写出表达式即可。 同时还需要记录每一次选择的是W,T还是L

PAT (Advanced Level) Practice

1001:  题目大意: 计算 a+b 的结果,并以标准格式输出——即每三个数字一组,组之间用逗号分隔(如果数字少于四位,则不需要逗号分隔)  解析: 我们知道相加右正有负,对于样例来说 Sample Input: -1000000 9 Sample Output: -999,991 如果是从左往右,算上负号的话输出应该是-99,999,1 从右往左:-,999,991离正确

HDU 1052(贪心)

题意:田忌赛马。田忌和齐王各有n匹马,输入田忌的马的速度和齐王的马的速度。每一轮田忌赢了就得200两银子,平就得0两,输了就失去200两银子。问田忌最多能得到多少。题目的策略是贪心,分析见leokan大牛的blog,这里也转一下,留存根。 http://hi.baidu.com/leokan/blog/item/126da06e1dab5ade80cb4a4f.html 算法可以用DP,

1050 String Subtraction——PAT甲级

Given two strings S1​ and S2​, S=S1​−S2​ is defined to be the remaining string after taking all the characters in S2​ from S1​. Your task is simply to calculate S1​−S2​ for any given strings. However,

【九度】题目1052:找x

题目1052:找x 时间限制:1 秒内存限制:32 兆特殊判题:否提交:4671解决:2504 题目描述: 输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。 输入: 测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。 输出: 对于每组输入,请输出结果。 样例输

1105 链表合并——PAT乙级

给定两个单链表 L1​=a1​→a2​→⋯→an−1​→an​ 和 L2​=b1​→b2​→⋯→bm−1​→bm​。如果 n≥2m,你的任务是将比较短的那个链表逆序,然后将之并入比较长的那个链表,得到一个形如 a1​→a2​→bm​→a3​→a4​→bm−1​⋯ 的结果。例如给定两个链表分别为 6→7 和 1→2→3→4→5,你应该输出 1→2→7→3→4→6→5。 输入格式: 输入首先在第一

1110 区块反转——PAT乙级

给定一个单链表 L,我们将每 K 个结点看成一个区块(链表最后若不足 K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。例如:给定 L 为 1→2→3→4→5→6→7→8,K 为 3,则输出应该为 7→8→4→5→6→1→2→3。 输入格式: 每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤105)、以及正整数 K (

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个