Bestcoder#54 A problem of sorting

A problem of sorting Accepts: 371 Submissions: 1706
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
给出一张许多人的年龄和生日表。你需要从年轻到年老输出人们的名字。(没有人年龄相同)
输入描述
第一行包含一个正整数T(T≤5),表示数据组数。
对于每组数据,第一行包含一个正整数n(1≤n≤100),表示人数,接下来n行,每行包含一个姓名和生日年份(1900-2015),用一个空格隔开。姓名长度大于0且不大于100。注意姓名中只包含字母,数字和空格。
输出描述
对于每组数据,输出nn行姓名。
输入样例
2
1
FancyCoder 1996
2
FancyCoder 1996
xyz111 1997
输出样例
FancyCoder
xyz111
FancyCoder

被题目坑了。。名字中有可能出现空格,所以需要用getline手动提取相应的信息出来。。用字符串的后四位计算出年份信息。
其他部分水过。

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

struct point
{
    int x;
    string s;
}p[105];



bool cmp(point a,point b)
{
    return a.x > b.x;
}

int main()
{
    string s;
    int n,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        getchar();
        for(int i = 0; i <n; i++)
        {
            getline(cin,s);
            int x = s.length();
            int o = 0;
            for(int j = x-4;j<=x-1;j++)
            {
                o = o*10+(s[j]-'0');
            }
            p[i].s = s.substr(0,x-5);
            p[i].x = o;
        }
        sort(p,p+n,cmp);
        for(int i = 0; i <n;i++)
            cout<<p[i].s<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值