PAT (Advanced Level) 1028. List Sorting (25)

时间卡的比较死,用string会超时。

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

struct X
{
    int id;
    char name[10];
    int score;
}s[100000+10];
int n,C;

bool cmp1(const X&a,const X&b){return a.id<b.id;}
bool cmp2(const X&a,const X&b)
{
    if(strcmp(a.name,b.name)==0) return a.id<b.id;
    return strcmp(a.name,b.name)<0;
}
bool cmp3(const X&a,const X&b)
{
    if(a.score==b.score) return a.id<b.id;
    return a.score<b.score;
}

int main()
{
    scanf("%d",&n); scanf("%d",&C);
    int tot=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i].id);
        scanf("%s",s[i].name);
        scanf("%d",&s[i].score);
    }
    if(C==1) sort(s+1,s+1+n,cmp1);
    else if(C==2)sort(s+1,s+1+n,cmp2);
    else sort(s+1,s+1+n,cmp3);
    for(int i=1;i<=n;i++)
        {
        printf("%06d ",s[i].id);
        printf("%s ",s[i].name);
        printf("%d\n",s[i].score);
    }
    return 0;
}

 

posted @ 2016-05-19 19:43  Fighting_Heart  阅读(133)  评论(0编辑  收藏  举报