HDU1263 (水果)做题笔记

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

这题可以使用二维的map做出来,注意迭代器的使用。

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
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
int main() {
int n,m,cnt;
char fruit[90],prov[90];
std::map<std::string, std::map<std::string, int> > tab;
std::map<std::string, std::map<std::string, int> >::iterator it1;
std::map<std::string, int>::iterator it2;
scanf("%d",&n);
while (n--) {
scanf("%d",&m);
tab.clear();
for (int i=0;i<m;i++) {
scanf("%s%s%d",fruit,prov,&cnt);
tab[prov][fruit]+=cnt;
}
for (it1=tab.begin();it1!=tab.end();it1++) {
printf("%s\n",it1->first.c_str());
for (it2=it1->second.begin();it2!=it1->second.end();it2++) {
printf(" |----%s(%d)\n",it2->first.c_str(),it2->second);
}
}
if (n) printf("\n");
}
}