主要是想学学map
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <string.h>
using namespace std;
const int N = 20;
const int INF = 10000000;
map <string, int> M;
int main()
{
// freopen("in.txt", "r", stdin);
int T, row, line;
string st, ans;
while(~scanf("%d", &T) && T)
{
M.clear();
while(T --)
{
cin >> st;
M[st] ++;
}
map <string, int> :: iterator it;
int maxx = 0;
for(it = M.begin(); it != M.end(); it ++)
{
if(maxx < it -> second)
{
maxx = it -> second;
ans = it -> first;
}
}
cout << ans << endl;
}
return 0;
}
两个贴在一起吧,供学习,都不是自己想出来的= =。做这道题以前居然忘了map内部的自动排序功能。而且嵌套两层后居然可以像二维数组一样,主要就是迭代器的改变。既然可以调用2层,那3层呢,4层呢,5。。。转存失败重新上传取消
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cctype>
#include <string.h>
using namespace std;
const int N = 3010;
const int INF = 10000000;
map <string, map<string, int> > M;
int main()
{
// freopen("in.txt", "r", stdin);
int n, m, num, flag = 0;
scanf("%d", &n);
string name, from;
while(n --)
{
if(flag) printf("\n");
flag = 1;
M.clear();
scanf("%d", &m);
while(m --)
{
cin >> name >> from >> num;
M[from][name] += num;
}
map <string, map<string, int> > :: iterator it;
map <string, int> :: iterator its;
for(it = M.begin(); it != M.end(); it ++)
{
cout << it->first << endl;
for(its = it->second.begin(); its != it->second.end(); its ++)
{
cout << " |----" << its->first << "(" << its->second << ")" << endl;
}
}
}
return 0;
}