排序 年轻的排前面 名字中可能有空格
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] = ‘\0‘ ; 38 } 39 40 sort(a , a+n , cmp) ; 41 for (i = 0 ; i < n ; i++) 42 printf("%s\n" , a[i].name) ; 43 44 } 45 46 return 0 ; 47 }
时间: 2024-10-11 00:25:01