hdu 5427 A problem of sorting

来源:互联网 发布:ubuntu安装虚拟机 编辑:程序博客网 时间:2024/05/16 07:17

点击打开链接

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 \leq 100T100 which denotes the number of test cases.

For each test case, there is an positive integer n (1 \leq n \leq 100)n(1n100) which denotes the number of people,and next nn 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 100100.Notice name only contain letter(s),digit(s) and space(s).

Output

For each case, output nn lines.

Sample Input
21FancyCoder 19962FancyCoder 1996xyz111 1997

题目大意:就是让你排序,按照从大到小排序,然后输出姓名就行了

解题思路:注意这里有一个坑,就是可能有空格作为当前的名字比如说:  ITAK,输出的时候也需要输出空格,这就需要用到cin.getline了,这个就是能读单字符的。。。

读入啥,输出啥,可以手动实现一下:


上代码:

</pre><pre name="code" class="cpp"><pre name="code" class="cpp">/*Date : 2015-09-05 晚上Author : ITAKINGMotto :今日的我要超越昨日的我,明日的我要胜过今日的我;以创作出更好的代码为目标,不断地超越自己。*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;struct sa{    char name[105];    int date;} arr[105];int cmp(sa a, sa b){    return a.date > b.date;}char str[200];int main(){    int t,m;    scanf("%d",&t);    while(t--)    {        cin>>m;        getchar();        for(int i=0; i<m; i++)        {            cin.getline(str,200);//能够正常读入单字符            int len = strlen(str);            arr[i].date = str[len-4]*1000+str[len-3]*100+str[len-2]*10+str[len-1];            len -= 5;            str[len] = '\0';            strcpy(arr[i].name,str);        }        sort(arr, arr+m, cmp);        for(int i=0; i<m; i++)            cout<<arr[i].name<<endl;    }    return 0;}




0 0
原创粉丝点击