这道题是一个hash表的问题,可以作为理解hash的一道入门题
题意是说:给一系列互不相同的数字,从小到大找第一个没有出现的数字。
我们就可以开一个bool型(int型用0/1也是一个道理)的数组mark[i]来记忆i这个数字有没有出现过,然后读完了之后遍历,第一个false的就是所求的数字了
代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#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; }
最后
以上就是专注铅笔最近收集整理的关于【水hash】#27 A. Next Test的全部内容,更多相关【水hash】#27内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复