#5702:Solving Order(结构体排序,水题)

原题目链接

题目大意:把颜色由多到少进行排序,从大到小的输出。

解题思路:将变量存在结构体中,然后结构体排序即可。还需要注意格式的问题。

详见代码。

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

using namespace std;

struct node
{
    char ch[110];
    int num;
}s[110];

bool cmp(node a, node b)
{
    return a.num > b.num;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s%d", s[i].ch, &s[i].num);
        }
        sort(s, s + n, cmp);
        for (int i = 0; i < n; i++)
        {
            printf("%s%c", s[i].ch, i != n - 1 ? ' ' : '\n');
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/RioTian/p/12823296.html