結構體排序HDU 5702

Sample Input
3
3
red 1
green 2
yellow 3
1
blue 83
2
red 2
white 1
Sample Output
yellow green red
blue
red white
題意:
輸入一個字符串 一個數字
按照數字大小順序輸出字符。(從大到小)

#include<algorithm>
#include<cstdio>
using namespace std;
struct lkq
{
    char num[1000];
    int x;
}s[1005];
int cmp(lkq a,lkq b)
{
    return a.x>b.x;
}
int main()
{
    int t,i;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%s%d",s[i].num,&s[i].x);
        sort(s,s+n,cmp);
        for(i=0;i<n-1;i++)
        printf("%s ",s[i].num);
        printf("%s\n",s[i].num);
    }
}

int cmp(lkq a,lkq b)
{
return a.x>b.x;
}
排序數字,字符也跟着排序了。重點內容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章