hdu 5427 A problem of sorting(字符排序)
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
Source
具体看代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<cmath> 7 using namespace std; 8 #define N 106 9 int n; 10 struct Node{ 11 int year; 12 char ch[106]; 13 }person[N]; 14 bool cmp(Node a,Node b){ 15 return a.year>b.year; 16 } 17 int main() 18 { 19 int t; 20 scanf("%d",&t); 21 while(t--){ 22 scanf("%d",&n); 23 char s[150]; 24 getchar(); 25 for(int i=0;i<n;i++){ 26 person[i].year=0; 27 gets(s); 28 //puts(s); 29 int len=strlen(s); 30 for(int j=0;j<len-5;j++){ 31 person[i].ch[j]=s[j]; 32 } 33 person[i].ch[len-5]='\0';//记得加,否则wa 34 35 for(int j=len-4;j<len;j++){ 36 person[i].year=person[i].year*10+s[j]-'0'; 37 } 38 //printf("%s\n",person[i].ch); 39 //printf("%d\n",person[i].year); 40 } 41 sort(person,person+n,cmp); 42 for(int i=0;i<n;i++){ 43 printf("%s\n",person[i].ch); 44 } 45 } 46 47 return 0; 48 }