avatar
fireworks99
keep hungry keep foolish

HDU 1263 水果

Description

Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1263

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Sample Output

guangdong |----pineapple(5) |----sugarcane(1) shandong |----apple(3)
  • 1
  • 2
  • 3
  • 4
  • 5

关于map

map中的元素是自动按Key升序排序

用insert函数插入数据,在数据的 插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对 应的值

Code

#include<map> #include<string> #include<iostream> using namespace std; int main() { int t,n; int cnt; string fruit,address; cin >> t; while(t--) { map<string, map<string,int> >mm; map<string, map<string,int> >::iterator itw; map<string,int>::iterator itn; cin >> n; while(n--) { cin >> fruit >> address >> cnt; mm[address][fruit] += cnt;///精华 } for(itw = mm.begin(); itw != mm.end(); itw++) { cout << itw -> first << '\n'; for(itn = itw -> second.begin(); itn != itw -> second.end(); itn++) { cout << " |----" << itn -> first << "(" << itn -> second << ")" << '\n'; } } if(t!=0) cout << '\n'; } return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy