这道题是一个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;
}

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

被折叠的 条评论
为什么被折叠?



