水果

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. linux下的 sudo ln -s 源文件 目标文件

    这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件或目录在另外一个位置建立一个同步的链接,类似Windows下的超级链接. 这个命令最常用的参数是-s,具体用法是:sudo l ...

  2. RFX2401C与RFX2402E的区别

    随着科技的发展,射频设备也慢慢的普及,射频放大器在射频设备中起着非常重要的作用.为了能获得足够大的距离,必须都要外加射频信号放大器. 射频信号放大器简称 “PA”.PA主流应用主要有ZigBee .无 ...

  3. 深入理解java虚拟机第五部分高效并发

    volatile是java虚拟机提供最轻量级的同步机制. volatile两个特性:1,保证同步的变量对所有线程是可见的.虽然对所有线程是即时可见的,但是却不保证原子性,也就是不保证线程安全,比如对于 ...

  4. CocoaPods安装/卸载/初始化等常用操作

    CocoaPods的官网:https://cocoapods.org/,官方指导文档https://guides.cocoapods.org/ 1)ruby gem源更换国内源gems.ruby-ch ...

  5. 关于构造函数中的this()和super()

    今天看到一个这段代码 public DataSourcePool(String driver, String url, String user, String pwd) throws Exceptio ...

  6. Tensorflow学习教程------变量

    #coding:utf-8 import tensorflow as tf x = tf.Variable([1,2]) a = tf.constant([3,3]) #增加一个减法op sub = ...

  7. delphi 文本 记录 流式 读写文件

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  8. request对象和response对象的作用和相关方法

    response对象(响应) 响应行 状态码 :setStatus(int a) 设置状态码 302重定向 304控制缓存 响应头 setHeader() 一个key对应一个value addHead ...

  9. Mac OS/Windows好用软件分享

    下软件全部为破解版,仅供参考学习用,如涉及商业. 请支持正版!谢谢 全部为本人亲测过 看上哪个留言发给你!   直接全分享上来会有人居心不良!

  10. dp--P1439 最长公共子序列(LCS)

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入格式 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式 一个数,即最长公共子序列的长度 找出两 ...