要求:
https://www.dotcpp.com/oj/problem1102.html?sid=1913813&lang=1#editor
这一题很简单,一种方法用set,一种用hash,主要记录下使用hash的方法
代码:
#include <stdio.h>
int main(){
bool hhash[1000] = {false};
/*
for(int i = 0;i <1000;i++){
hhash[i] = false;
}
*/
int n,m,cnt = 0;
scanf("%d",&n);
for(int i = 0;i <n;i++){
scanf("%d",&m);
if(hhash[m] == false){ //若一开始是false,则累加
cnt++;
}
hhash[m] = true;
}
printf("%d\n",cnt);
int signal = 0;
for(int j = 0;j <1000;j++){
if(hhash[j] == true){
printf("%d",j); //
if(signal <cnt - 1){
printf(" ");
}
signal++;
}
}
}
/*
10
20 40 32 67 40 20 89 300 400 15
*/
使用set:
#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
int main(){
int n,m;
set<int>num;
scanf("%d",&n);
for(int i = 0;i <n;i++){
scanf("%d",&m);
num.insert(m);
}
printf("%d\n",num.size());
for(set<int>::iterator it = num.begin();it != num.end();it++){
printf("%d",*it);
if(it != num.end()) printf(" ");
}
}