【水hash】#27 A. Next Test

本文介绍了一种使用哈希表解决寻找最小未使用正整数的方法。通过遍历输入的已使用索引并标记,最终找到第一个未被标记的整数作为新测试案例的默认索引。

A. Next Test
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

«Polygon» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated as the smallest positive integer which is not used as an index for some previously added test.

You are to implement this feature. Create a program which determines the default index of the next test, given the indexes of the previously added tests.

Input

The first line contains one integer n (1 ≤ n ≤ 3000) — the amount of previously added tests. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 3000) — indexes of these tests.

Output

Output the required default value for the next test index.

Sample test(s)
input
3
1 7 2
output
3


这道题是一个hash表的问题,可以作为理解hash的一道入门题

题意是说:给一系列互不相同的数字,从小到大找第一个没有出现的数字。

我们就可以开一个bool型(int型用0/1也是一个道理)的数组mark[i]来记忆i这个数字有没有出现过,然后读完了之后遍历,第一个false的就是所求的数字了

代码如下:

#include <cmath> 
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

bool mark[3001];

int main()
{
	memset(mark,false,sizeof(mark));
	int n=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int now;
		cin>>now;
		mark[now]=true;
	}
	
	for(int j=1;j<=n+1;j++)
	{
		//cout<<mark[j];
		if(mark[j]==false)
		{
			cout<<j;
			return 0;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值