zoukankan      html  css  js  c++  java
  • hdu1263 简单模拟

    题意:依据水果销量表。依照特定格式输出

    格式:首先按产地排序,然后同一产地按水果名排序

    注意:第一,设计多级排序

              第二。同一产地同一水果可能多次出现,所以须要在前面已经输入的水果里面遍历找一次

              第三,这里

    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int flag;//不同产地不同名的水果种类数量
    
    struct Node{
        string name,id;
        int sum;
        bool operator<(const Node &N)const
        {
            if(id == N.id) return name < N.name;
            else return id < N.id;
        }
    }node[105];
    
    void print(int M){
        for(int i = 0; i < flag; )
        {
            string id = node[i].id;
            cout << id << endl;
            cout <<"   |----" << node[i].name << '(' << node[i].sum <<')' <<endl;
            i ++;
            while(id == node[i].id && i < M)
            {
                cout <<"   |----" << node[i].name << '(' << node[i].sum <<')' <<endl;
                i ++;
            }
        }
    }
    
    int main()
    {
        int N,M;
        cin >> N;
        string name,id;
        int sum;
        while(N --)
        {
            cin >> M;
            flag = 0;
            for(int i = 0; i < M; i ++)
            {
                cin >> name >> id >> sum;
                int j;
                for(j = 0; j < i; j ++) //在前面已有的水果中查找一次
                {
                    if(name == node[j].name && id == node[j].id)
                    {
                        node[j].sum += sum;
                        break;
                    }
                }
                if(i == j) //在之前的水果中没找到,那么就是一种新的水果
                    node[flag].id = id,node[flag].name = name,node[flag++].sum = sum;
            }
            sort(node,node+flag);
            print(M);
            if(N != 0) cout << endl;//两个案中间空行例
        }
        return 0;
    }
    


查看全文
  • 相关阅读:
    Python 集合
    Python sorted()
    CodeForces 508C Anya and Ghosts
    CodeForces 496B Secret Combination
    CodeForces 483B Friends and Presents
    CodeForces 490C Hacking Cypher
    CodeForces 483C Diverse Permutation
    CodeForces 478C Table Decorations
    CodeForces 454C Little Pony and Expected Maximum
    CodeForces 313C Ilya and Matrix
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10944202.html
  • Copyright © 2011-2022 走看看