題目描述
先輸入一組數,然後輸入其分組,按照分組統計出現次數並輸出,參見樣例。
輸入
輸入第一行表示樣例數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}
思路
雖然知道n的範圍,但是不知道其中數的範圍,
這裏藉助C++中STL的 集合和映射等容器,統計一下即可
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m, n;
cin >> m;
while(m--)
{
cin >> n;
vector<int>a(n), b(n);
set<int>se;
set<int>se1;
unordered_map<int, vector<int>>ma;
for(int i=0; i<n; ++i)
{
cin >> a[i];
se1.insert(a[i]);
}
for(int i=0; i<n; ++i)
{
cin >> b[i];
se.insert(b[i]);
ma[b[i]].push_back(a[i]);
}
set<int>::iterator it = se.begin();
for(it=se.begin(); it!=se.end(); ++it)
{
cout << *it << "={";
for(set<int>::iterator it1 = se1.begin(); it1!=se1.end(); it1++)
{
int count = 0;
for(int j=0; j<ma[*it].size(); ++j)
{
if(ma[*it][j] == *it1)
count ++;
}
cout<< *it1 << "="<< count;
if((++it1)!=se1.end())
cout << ",";
it1--;
}
cout << "}" << endl;
}
}
return 0;
}