hdu 5427 A problem of sorting 排序

A problem of sorting

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


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
 

Source
BestCoder Round #54 (div.2)



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define maxn 101
int year[maxn],num[maxn];
char name[maxn][107];


int comp(int a,int b){
    return year[a] > year[b];
};

int main(){
    int t, n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        getchar();
        for(int i= 0;i <n ; i++){
            num[i] = i;
            int len = 0;
            while(1){
                name[i][len] = getchar();
                if(name[i][len] == '\n') break;
                len++;
            }
            name[i][len-5]= '\0';
            year[i] = 0;
            for(int j = len-4;j < len; j++)
                year[i] = year[i]*10+name[i][j]-'0';
        }
        sort(num,num+n,comp);
        for(int i = 0;i < n; i++){
            printf("%s\n",name[num[i]]);
        }
    }
    return 0;
}


你可能感兴趣的:(排序,HDU,problem,of,sorting,a,5427)