zoukankan      html  css  js  c++  java
  • #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 ? ' ' : '
    ');
            }
        }
        return 0;
    }
    
  • 相关阅读:
    输出流
    异常处理
    异常限制
    多个受控异常
    跟踪异常传播
    动手动脑练习2
    动手动脑练习
    文件总结
    程序员修炼之道3
    Shell教程 之printf命令
  • 原文地址:https://www.cnblogs.com/RioTian/p/12823296.html
Copyright © 2011-2022 走看看