(算法练习)——问题 1102: 明明的随机数

要求:
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(" ");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值