HDU 5427 A problem of sorting 水题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 T100 which denotes the number of test cases. 

For each test case, there is an positive integer n(1n100) 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 }
View Code

 

以上是关于HDU 5427 A problem of sorting 水题的主要内容,如果未能解决你的问题,请参考以下文章

hdu 4506(数学,循环节+快速幂)

hdu 2256 Problem of Precision

hdu 2256 Problem of Precision

HDU2256-Problem of Precision(矩阵构造+高速幂)

hdu-2256 Problem of Precision---矩阵快速幂+数学技巧

bzoj5427最长上升子序列(贪心+LIS)