水果

http://acm.hdu.edu.cn/showproblem.php?pid=1263

Problem Description

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

Input

第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.

Output

对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.两组测试数据之间有一个空行.最后一组测试数据之后没有空行.

Sample Input


apple shandong
pineapple guangdong
sugarcane guangdong
pineapple guangdong
pineapple guangdong

Sample Output

guangdong
|----pineapple()
|----sugarcane()
shandong
|----apple()

map的嵌套

 #include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <map> using namespace std; int main()
{
//freopen("sample.txt","r",stdin);
int n;
cin>>n;
for(int i=;i<n;i++)
{
if(i!=)
cout<<endl;
int m;
cin>>m;
map<string,map<string,int> > mp;
map<string,map<string,int> >::iterator it1;
map<string,int>::iterator it2;
while(m--)
{
string fruit,address;
int count;
cin>>fruit>>address>>count;
mp[address][fruit]+=count; //这一步要记住
}
for(it1=mp.begin();it1!=mp.end();it1++)
{
cout<<it1->first<<endl;
for(it2=it1->second.begin();it2!=it1->second.end();it2++)
{
cout<<" |----"<<it2->first<<"("<<it2->second<<")"<<endl;
}
}
}
return ;
}
别人用了struct间接实现map的嵌套
 #include<iostream>
#include<string>
#include<map>
using namespace std; struct MyStruct
{
map <string, int>MyStructma; //存放水果名以及该种水果的数量
};
int main()
{
map <string, MyStruct>ma; //地名
map <string, MyStruct>::iterator it;
map <string, int>::iterator MyStructmait;
string fruit,place;
int count;
int n,t;
cin>>t;
while(t--)
{
ma.clear();
cin>>n;
while(n--)
{
cin>>fruit>>place>>count;
ma[place].MyStructma[fruit] += count;
}
for (it = ma.begin(); it != ma.end(); it++)
{
cout<<it->first<<endl;
for (MyStructmait = it->second.MyStructma.begin(); MyStructmait != it->second.MyStructma.end(); MyStructmait++)
{ cout<<" |----"<<MyStructmait->first<<"("<<MyStructmait->second<<")"<<endl;
}
}
if(t != )cout<<endl;
}
return ;
}

也可以结构体排序,不用STL

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct Node
{
char name[];
char space[];
int num;
} f[]; int cmp(Node x,Node y)
{
if(strcmp(x.space,y.space))
return strcmp(x.space,y.space)<;
return strcmp(x.name,y.name)<;
} int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%*c",&n);
for(i = ; i<n; i++)
{
scanf("%s%s%d",f[i].name,f[i].space,&f[i].num);
}
sort(f,f+n,cmp);
char di[],min[];
int cnt = ,flag = ;
strcpy(di,f[].space);
strcpy(min,f[].name);
for(i = ; i<n; i++)
{
if(strcmp(di,f[i].space))
{
strcpy(di,f[i].space);
strcpy(min,f[i].name);
flag = ;
cnt = ;
}
if(!strcmp(di,f[i].space))
{
if(flag)
{
printf("%s\n",di);
flag = ;
}
if(!strcmp(min,f[i].name))
{
while(!strcmp(min,f[i].name) && !strcmp(di,f[i].space))//产地与水果名都必须相同
{
cnt+=f[i].num;
i++;
}
printf(" |----%s(%d)\n",min,cnt);
strcpy(min,f[i].name);
i--;
cnt = ;
}
}
}
if(t)
printf("\n");
} return ;
}

另一种解法

 #include<cstdio>
#include<iostream>
#include<map>
#include<string>
using namespace std; int main(){
int n,m;
string color;
map<string,int>mp;
while(scanf("%d",&m)!=EOF && m){
while(m--){
cin>>color;
mp[color]++;
}
int max = ;
string a;
for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
if(max<=it->second){
max = it->second;
a = it->first;
}
}
cout<<a<<endl;
mp.clear();
}
return ;
}

STL中map的嵌套使用

from:https://www.cnblogs.com/1114250779boke/archive/2012/08/07/2626477.html

 
最近开发中要用到STL,然后自己查看了一些资料,并写了一些代码。在使用<map>中,想起了如果是map嵌套,该如何应用呢?下面是我的coding内容:

对于传统的map:

 #include <iostream>
#include <map> using namespace std; int main()
{
map<int, string> scores; scores.insert(make_pair(,"maxi")); scores[]="MAXI"; scores.insert(make_pair(,"xiaoyu")); scores.insert(make_pair(,"xiao")); scores[]="xiaoma"; map<int,string>::iterator pScores; for(pScores=scores.begin();pScores!=scores.end();pScores++)
{
std::cout<<pScores->first<<" "<<pScores->second<<endl;
}
return ;
}

结果输出:

    

由此可以看出,scores[100]="MAXI"会直接替换掉原来100map对应的value,而如果调用scores.insert()函数,则由于本map是单映射的,300 map的value:xiao就不会替换掉原来300 map对应的value:xiaoyu

但如果我想定义嵌套的map并对它进行遍历,该如何进行呢:

 #include<iostream>
#include<map> using namespace std;
int main()
{
//对于这样的map嵌套定义,有两种插入方法:
//定义一个map<int, string>变量,对其定义后在插入multiMap
map<int,map<int,string> >multiMap; map<int, string> temp; temp.insert(make_pair(,"hi")); temp.insert(pair<int,string>(,"maxi")); //pair<int,string>()和make_pair()有相同作用 multiMap.insert(make_pair(, temp)); //将临时变量插入到multiMap中
//也可以直接赋值
multiMap[][]="xiaoyu"; multiMap[][]="xiaoma"; // 以下是如何遍历本multiMap
map<int,map<int,string> >::iterator multitr;
map<int,string>::iterator intertr;
for(multitr=multiMap.begin();multitr!=multiMap.end();multitr++)
{
for(intertr= multitr ->second.begin(); intertr != multitr ->second.end(); intertr ++)
std::cout<< multitr ->first<<" "<<intertr->first<<" ("<< intertr -> second <<")"<<endl;
}
return ;
}

运行结果如下:

      

总结,map的成员加入有两种赋值方法,一种是调用map.insert()函数,这样,由于是单映射,后面加入的新的pair对如果有key值和前面一样,那么后面的pair对元素将不会被加入到map中;但如果是直接[ ]=赋值操作的话,相当于数组赋值,会直接替换掉原来具有相同key域的pair对。


map的嵌套 + 例题(水果)的更多相关文章

  1. 水果(map的嵌套)

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

  2. Map的嵌套 练习

    Map的嵌套   练习 利用迭代和增强for循环的两种方式实现如下效果 package cn.ccc; import java.util.HashMap;import java.util.Iterat ...

  3. Map的嵌套,HDU(1263)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 新学的map的嵌套 #include <stdio.h> #include < ...

  4. 双列集合Map的嵌套遍历

    双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...

  5. Map接口----Map中嵌套Map

    package cn.good.com; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impo ...

  6. Map的嵌套

    package cn.lijun.demo2; import java.util.HashMap; import java.util.Iterator; import java.util.Set; p ...

  7. Map的嵌套使用

    Map嵌套Map: 例: AAA: Javas班: 001 熊大 002 熊二 Hdoop班 001 小猪猪 002 小菲菲 ★使用增强for循环遍历Set数组: import java.util.H ...

  8. fastjson排序 Map多层嵌套转换自动排序问题终极解决方案

    阅读更多 最近项目中用到了fastjson(1.2.15)需要将前端多层嵌套json转换为map,由于map的无序性,想了很多办法,最终找到使用 Map m= JSONArray.parseObjec ...

  9. map练习小例题

    "fdgavcbsacdfs" 获取该字符串中,每一个字母出现的次数. 要求打印结果是:a(2)b(1)...; 思路: 对于结果的分析发现,字母和次数之间存在着映射关系.而且这种 ...

随机推荐

  1. 再战希捷:西部数据透露96层闪存已用于消费级SSD

    导读 96层堆叠3D NAND闪存已经成为行业主流,包括西部数据这样的传统机械硬盘大厂,也在逐步普及96层闪存,并已经用于消费级SSD. 96层堆叠3D NAND闪存已经成为行业主流,包括西部数据这样 ...

  2. PythonTwo

    格式化输出: % 占位符  s(str 字符串) d(digit 数字)  %% 只单纯显示% Str 索引切片 captlze  首字母大写 upper 全大写 lower 全小写 find 通过元 ...

  3. monkey命令详解《转载》

    monkey命令详解: https://blog.csdn.net/a136332462/article/details/76014412

  4. SqlServer 集合运算符

    1.集合运算符概述 (1)集合运算符运用与集合之间的运算. (2)多元集合: 指的是来自两个输入查询的集合,可能包含重复项 (3)T-SQL 支持三种集合运算符 union .intersect .e ...

  5. Tomcat9卸载与安装

    Tomcat9卸载与安装 首先确定环境变量配置正确,按实际的安装路径来设置. 在tomcat9的bin目录下打开命令行窗口 按住shift键不放,点击右键 输入以下命令 在打开的命令行窗口中输入以下命 ...

  6. 手把手教你用Python实现“坦克大战”,附详细代码!

    小时候玩的“坦克大战”,你还记得吗? ​ 满满的回忆 ! 今天,我们使用Python以及强大的第三方库来实现一个简单的坦克大战游戏. ​ 整体效果 环境依赖 python3.7 pygame1.9.6 ...

  7. Failed to connect to raw.githubusercontent.com port 443: Connection refused

    问题:macOS安装Homebrew时总是报错(Failed to connect to raw.githubusercontent.com port 443: Connection refused) ...

  8. 吴裕雄--天生自然 PHP开发学习:PHP编程基础知识

    <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> <?php $txt="Hello world!"; $x=5; $y=10.5; ...

  9. 寒假day26

    根据已有数据爬取新数据充实人才库

  10. ansible异步任务

    转载于简书博客 https://www.jianshu.com/p/3962bf94ae70 ansible方便在于能批量下发,并返回结果和呈现.简单.高效. 但有的任务执行起来却不那么直接,可能会花 ...