出题人题解|T1
2024-11-21 15:45:59
发布于:香港
44阅读
0回复
0点赞
方法一:使用数组统计
观察到 ,所以可以开一个长度为 的数组进行统计。
#include <bits/stdc++.h>
using namespace std;
int n;
bool cnt[100010];
int main(){
cin >> n;
while(n -- ){
int x;
cin >> x;
cnt[x] ++;
}
cout << accumulate(cnt + 1, cnt + 1 + 100000, 0);
return 0;
}
方法二:使用set统计
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
set<int>s;
cin >> n;
while(n -- ){
int x;
cin >> x;
s.insert(x);
}
cout << s.size();
return 0;
}
方法三:使用map统计
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
map<int,int>mp;
cin >> n;
while(n -- ){
int x;
cin >> x;
mp[x] ++;
}
cout << mp.size();
return 0;
}
这里空空如也
有帮助,赞一个