标签:
Description
给出一串数字,这串数字由 n 个数 ai 组成,找出未出现在这串数字中的最小正整数
Input
输入第一行为一个正整数 n (1 <= n <= 1000)
第二行为 n 个正整数 ai ( 1 <= ai <= 1000000000)
Output
输出没有出现在这 n 个正整数中的最小的正整数
Sample Input
5
2 4 3 5 1
5
1 100 101 102 103
Sample Output
6
2
思路
因为n的大小为1000,所以没有出现在n个整数中的最小整数必定在1-1001中产生,开一个大小为1000的数组标记1-1000中数存在的情况,遍历一遍就可以了。
#include<stdio.h>
int main()
{
int N;
while (~scanf("%d",&N))
{
int ans[1005] = {0},tmp,i;
bool flag = false;
for (i = 0;i < N;i++)
{
scanf("%d",&tmp);
if (tmp <= 1000)
{
ans[tmp] = 1;
}
}
for (i = 1;i <= N;i++)
{
if (!ans[i])
{
flag = true;
printf("%d\n",i);
break;
}
}
if (!flag)
{
printf("%d\n",N + 1);
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/zzy19961112/p/5831845.html