HDU 5427 A problem of sorting

简单的名次排序问题
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005
char s[N];
struct node
{
    char name[N];
    int socre;
}t[N];
int cmp(node p,node q)
{
    return p.socre>q.socre;//从大到小排序
}
int main()
{
    int y;
    scanf("%d",&y);
    while(y--)
    {
        int n,i;
        scanf("%d",&n);
        getchar();//把回车吃掉
        for(i=0;i<n;i++)
        {
            gets(s);
            int len=strlen(s);
            t[i].socre=0;
            for(int j=len-4;j<=len-1;j++)
            {
                t[i].socre=10*t[i].socre+(s[j]-'0');
            }
            for(int j=0;j<=len-5;j++)
            {
                    t[i].name[j]=s[j];
            }
            t[i].name[len-5]='\0';//必须有这步,错了N次,就是因为这个地方没有。这步表示字符串的结束
        }
        sort(t,t+n,cmp);
        for(i=0;i<n;i++)
        {
            printf("%s\n",t[i].name);
        }
    }
    return 0;
}

你可能感兴趣的:(HDU 5427 A problem of sorting)