HDU 5427 A problem of sorting 水题

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5427

A problem of sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1447    Accepted Submission(s): 554

Problem Description

There are many people‘s name and birth in a list.Your task is to print the name from young to old.(There is no pair of two has the same age.)

Input

First line contains a single integer T≤100 which denotes the number of test cases.

For each test case, there is an positive integer n(1≤n≤100) which denotes the number of people,and next n lines,each line has a name and a birth‘s year(1900-2015) separated by one space.

The length of name is positive and not larger than 100.Notice name only contain letter(s),digit(s) and space(s).

Output

For each case, output n lines.

Sample Input

2

1

FancyCoder 1996

2
FancyCoder 1996

xyz111 1997

Sample Output

FancyCoder

xyz111

FancyCoder

题解:

  被水题pe了一早上,学了个fgets来取代gets,据说后者不太安全,最好不要用。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn = 111;
 8
 9 char name[maxn][maxn];
10 int age[maxn];
11 int ran[maxn];
12
13 int n;
14
15 bool cmp(int x, int y) {
16     return age[x] > age[y];
17 }
18
19 void init() {
20
21 }
22
23 int main() {
24     int tc;
25     scanf("%d", &tc);
26     while (tc--) {
27         //用scanf("%d\n",&n);代替以下两行会PE。
28         scanf("%d", &n);
29         getchar();
30         for (int i = 0; i < n; i++) {
31             //fgets得到的字符串,在‘\0‘之前会多一位‘\n‘!
32             fgets(name[i], sizeof(name[i]), stdin);
33 //            gets(name[i]); 不安全,不要用
34             int len = strlen(name[i]);
35             age[i] = 0;
36             for (int j = len - 5; j < len-1; j++) {
37                 age[i] = age[i] * 10 + name[i][j] - ‘0‘;
38             }
39             name[i][len - 6] = ‘\0‘;
40         }
41         for (int i = 0; i < n; i++) {
42             //printf("str:%s.age:%d\n", name[i],age[i]);
43         }
44         for (int i = 0; i < n; i++) ran[i] = i;
45         sort(ran, ran + n, cmp);
46         for (int i = 0; i < n; i++) {
47             int x = ran[i];
48             printf("%s\n", name[x]);
49         }
50     }
51     return 0;
52 }

时间: 2024-11-06 07:30:19

HDU 5427 A problem of sorting 水题的相关文章

hdu 5427 A problem of sorting

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5427 A problem of sorting Description There are many people's name and birth in a list.Your task is to print the name from young to old.(There is no pair of two has the same age.) Input First line contai

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is

HDU 4143 A Simple Problem(数论-水题)

A Simple Problem Problem Description For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2. Input The first line is an integer T, which is the the number of cases. Then T line

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

HDU 1800 Flying to the Mars (水题)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11099    Accepted Submission(s): 3572 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou

HDU 5344 MZL&#39;s xor(水题)

MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 456    Accepted Submission(s): 322 Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to

hdu 4274 Spy&#39;s Work(水题)

Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 388 Problem Description I'm a manager of a large trading company, called ACM, and responsible for the