hdu 5427(排序水题)

排序 年轻的排前面 名字中可能有空格

Sample Input
2
1
FancyCoder 1996
2
FancyCoder 1996
xyz111 1997

Sample Output
FancyCoder
xyz111
FancyCoder

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <cmath>
 7 # include <queue>
 8 # define LL long long
 9 using namespace std ;
10 
11 struct ren
12 {
13     char name[200] ;
14     int age ;
15 }a[120];
16 
17 bool cmp(ren x , ren y)
18 {
19     return x.age > y.age ;
20 }
21 
22 int main ()
23 {
24     //freopen("in.txt","r",stdin) ;
25     int T ;
26     scanf("%d" , &T) ;
27     while(T--)
28     {
29         int n , i ;
30         scanf("%d" , &n) ;
31         getchar() ;
32         for (i = 0 ; i < n ; i++)
33         {
34             gets(a[i].name) ;
35             int len = strlen(a[i].name) ;
36             sscanf(&a[i].name[len-5] , "%d" , &a[i].age);
37             a[i].name[len-5] = '' ;
38         }
39 
40         sort(a , a+n , cmp) ;
41         for (i = 0 ; i < n ; i++)
42             printf("%s
" , a[i].name) ;
43 
44     }
45 
46     return 0 ;
47 }
View Code
原文地址:https://www.cnblogs.com/mengchunchen/p/4823540.html