水果(map的嵌套)

夏天来了~~好开心啊,呵呵,好多好多水果~~ 
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

Sample Output

guangdong
   |----pineapple(5)
   |----sugarcane(1)
shandong
   |----apple(3)
 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5 map<string, map<string, int >> mp;
 6 int main()
 7 {
 8     int t,mark=0;
 9
10     cin >> t;
11     while (t--)
12     {
13         mp.clear();
14         if (mark) cout << endl;
15         mark = 1;
16         int n;
17         cin >> n;
18         for (int i = 0; i < n; i++)
19         {
20             string c = "   |----";
21             string a, b;
22             int t;
23             cin >> a >> b >> t;
24             c += a;
25             mp[b][c] += t;
26         }
27         map<string, map<string, int>> ::iterator p;
28         for (p = mp.begin(); p != mp.end(); p++)
29         {
30             cout << p->first<<endl;
31             map<string, int >::iterator k;
32             for (k = p->second.begin(); k != p->second.end(); k++)
33             {
34                 cout << k->first << "(" << k->second << ")"<<endl;
35             }
36         }
37     }
38     return 0;
39 }

题解:可用map的嵌套,也可用map中嵌套pair(没有试,下次可以试试),经典水题;

注意的地方是迭代器的嵌套使用,以及map中的clear操作

原文地址:https://www.cnblogs.com/kangdong/p/8454640.html

时间: 2024-08-04 14:45:03

水果(map的嵌套)的相关文章

学 Java的第30天 Map中嵌套Map

package cn.aaa; import java.util.Iterator;import java.util.Map.Entry;import java.util.HashMap;import java.util.Set; //Map中嵌套存储Map//aaa//java班//  001 周一//  002 周二//hoodp班//  001 周三//  002 周四////java班 :存学号和名字//hoodp班:存学号和名字//aaa:存的是班级//  java班<学号,姓名>/

计蒜客练习题:水果店(嵌套map)

这道题主要就是考察嵌套map的使用 这道题我一开始用的map+pair但是怎么写都不对. 之后就求助万能的百度发现网上都是用嵌套map做的. 别说我还真不知道map该怎么嵌套,以及怎么调用嵌套map. map<string, map<string,int> > m; 定义在第二维,插入的话就跟二维数组一样(m[tm1][tm2] = tm3;). 调用的话需要定义两个不同的iterator一个是外层的,一个是内层的. 具体看本题代码. PS:注意输出格式! 附AC代码: #incl

c++ map多重嵌套【转】

最近开发中要用到STL,然后自己查看了一些资料,并写了一些代码.在使用<map>中,想起了如果是map嵌套,该如何应用呢?下面是我的coding内容: 对于传统的map,我们只需要: #include<map> #include<iostream> int main() { map<int, string> scores; scores.insert(make_pair(100,"maxi")); scores[100]="MA

uva 11991 (map vector 嵌套)

其实这题可以直接用vector #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<map> #include<vector> using namespace std; vector<int> str[1000000+100]; int main() { int n,m; int i,j,k; int que,

Java 集合-Map集合嵌套 的遍历四种方式

1 public class Person { 2 private String name; 3 private Integer age; 4 public String getName() { 5 return name; 6 } 7 public void setName(String name) { 8 this.name = name; 9 } 10 public Integer getAge() { 11 return age; 12 } 13 public void setAge(I

第3章 Map集合的嵌套

1.1 Map中嵌套Map1.1.1 案例代码十一 package com.itheima_03; public class Student { private String num; private String name; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String num, String name) { super(); this.num = num;

bboss oreach循环嵌套遍历map

foreach循环嵌套遍历mapforeach嵌套dsl脚本定义 <property name="dynamicInnerDsl"> <![CDATA[{ ## 最多返回1000条记录 size: #[size], "query": { "bool": { "must": [ #set($needComma = false) #foreach($condition in $conditions.entrySe

Java基础Map接口+Collections

1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Map集合的添加 */ Map<String, String> map = new HashMap<String, String>(); map.put("星期一", "Monday"); map.put("星期六", "

JAVA基础学习day16--集合三-Map、HashMap,TreeMap与常用API

一.Map简述 1.1.简述 public interface Map<K,V> 类型参数: K - 此映射所维护的键的类型 key V - 映射值的类型 value 该集合提供键--值的映射.key不能重复,一对对的存储方式 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. 1.2.方法 嵌套类摘要 static interface Map.Entry<K,V> 映射项(键-值对). 方法摘要 void clear() 从此映射中移除所有映射关系(可选操