题目描述:
先输入一组数,然后输入其分组,按照分组统计出现次数并输出,参见样例。
输入描述:
输入第一行表示样例数m,对于每个样例,第一行为数的个数n,接下来两行分别有n个数,第一行有n个数,第二行的n个数分别对应上一行每个数的分组,n不超过100。
输出描述:
输出m行,格式参见样例,按从小到大排。
输入
1
7
3 2 3 8 8 2 3
1 2 3 2 1 3 1
输出
1={2=0,3=2,8=1}
2={2=1,3=0,8=1}
3={2=1,3=1,8=0}
题目详情:
知识点:
- map中出现没有的键。输出的值对应为0(不会出现越界的错误)
- 熟练合理应用 map 和 set
代码
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<map>
#include<set>
using namespace std;
int main()
{
int sample;
int num;
set<int>a;
set<int>b;
cin >> sample;
while (sample--)
{
cin >> num;
int line1[num];
int line2[num];
a.clear();
b.clear();
int count = 0;
for (int i = 0; i < num; i++)
{
cin >> line1[i];
a.insert(line1[i]); // 利用set去掉重复的元素,只统计种类
}
for (int j = 0; j < num; j++)
{
cin >> line2[j];
b.insert(line2[j]); //利用set去掉重复的元素,只统计种类
}
map<int, int>totle;
for (set<int>::iterator it = b.begin(); it != b.end(); it++)
{
totle.clear();
cout << *it << "={";
for (int i = 0; i < num; i++)
{
if (*it == line2[i])
{
if (!totle.count(line1[i]))
totle[line1[i]] = 0;
totle[line1[i]]++;
}
}
int temp = 0;
for (set<int>::iterator at = a.begin(); at != a.end(); at++)
{
cout << *at << "=" << totle[*at];
temp++; // map中出现没有的键。输出的值对应为0(不会出现越界的错误)
if (temp != a.size())
cout << ",";
}
cout << "}" << endl;
}
}
return 0;
}