输入:数量n n个数据 数量q q个不重复的数据
输出:q的数据中和n数据重复的个数
Sample Input 1
5
1 2 3 4 5
3
3 4 1
Sample Output 1
3
Sample Input 2
3
3 1 2
1
5
Sample Output 2
0
Sample Input 3
5
1 1 2 2 3
2
1 2
Sample Output 3
2
因为限制条件中q比较小,直接线性搜索
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main (){
int n ,q, a,k=0;
cin>>n;
vector<int> A;
for(int i = 0;i<n;i++){
cin>>a;
A.push_back(a);
}
cin>>q;
for(int i=0;i<q;i++){
cin>>a;
for(int j=0;j<n;j++){
if(A[j]==a) {k++;break;}
}
}
cout<<k<<endl;;
return 0;
}
错点:
1.线性搜索时,找到第一个重复的就要break出来,避免后面重复数字又算一次
2.输出最后加换行