Problem Description 夏天来了好开心啊,呵呵,好多好多水果
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
Sample Output guangdong |----pineapple(5) |----sugarcane(1) shandong |----apple(3)
Source 浙江工业大学第四届大学生程序设计竞赛
思路
用一个二维映射储存数据,然后按格式输出即可。
AC代码
#include <iostream>
#include <map>
#include <string>
#define AUTHOR "HEX9CF"
using namespace std;
int main()
{
int n, f = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
if (f)
{
cout << endl;
}
int m;
map<string, map<string, int>> M;
cin >> m;
for (int j = 0; j < m; j++)
{
string name, place;
int amount;
cin >> name >> place >> amount;
M[place][name] += amount;
}
for (map<string, map<string, int>>::iterator it1 = M.begin(); it1 != M.end(); it1++)
{
cout << it1->first << endl;
for (map<string, int>::iterator it2 = it1->second.begin(); it2 != it1->second.end(); it2++)
{
cout << " |----" << it2->first << "(" << it2->second << ")" << endl;
}
}
f = 1;
}
return 0;
}