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): 435    Accepted Submission(s): 205


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
 
提议就是按出生大小排序,注意,第一个可能是空格
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

struct zc
{
    char a[200];
    int year;
} r[5000];

int cmp(zc a,zc b)
{
    return a.year>b.year;
}

int main()
{
    int T,n,t,m;
    scanf("%d",&T);
    while(T--)
    {
        t=0;
        scanf("%d",&n);
        getchar();//刚开始写了%d\n,妈蛋,一直格式错误
        m=n;
        while(n--)
        {
            gets(r[t].a);
            //puts(r[t].a);
            int l=strlen(r[t].a);
            r[t].year=0;

            for(int i=l-5;i<l;i++)
                r[t].year=r[t].year*10+(r[t].a[i]-'0');

            r[t].a[l-5]=0;
            t++;
        }
        sort(r,r+m,cmp);
        for(int i=0;i<m;i++)
            puts(r[i].a);
    }
    return 0;
}


你可能感兴趣的:(ACM)