hdu - 1263 水果

题目链接:https://vjudge.net/problem/HDU-1263

题目描述:

夏天来了~~好开心啊,呵呵,好多好多水果~~
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.

Input第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
Output对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
Sample Input

1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1

map嵌套就行了
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a)*maxn)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll ll_INF = 233333333333333;
string plc;
int main(void) {
    //std::ios::sync_with_stdio(false);
    map<string, map<string, int> > a; 
    //先定义一个水果名字->水果数量的映射,再定义一个产地->水果信息的映射
    int t;
    scanf("%d", &t);
    while(t--) {
        int n;
        scanf("%d", &n);
        string place, fruname;
        int cnt;
        for (int i = 0; i<n; ++i) {
            cin >> fruname >> place >> cnt;
            a[place][fruname] += cnt; //双重映射的一种访问方式
        }
        map<string, map<string, int> >::iterator it;
        for (it = a.begin(); it != a.end(); ++it) {
            cout << it->first << endl;
            map<string, int>::iterator it2;
            for (it2 = it->second.begin(); it2 != it->second.end(); ++it2)
                cout << "   |----" << it2->first << '(' << it2->second << ')' << endl;
        }
        a.clear();
        if (t)
            cout << endl;
    }
    return 0;
}   

  

猜你喜欢

转载自www.cnblogs.com/shuitiangong/p/12210419.html