1144. The Missing Number (20)

Dear 丶 2022-05-28 08:25 236阅读 0赞

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.

Output Specification:

Print in a line the smallest positive integer that is missing from the input list.

Sample Input:

  1. 10
  2. 5 -25 9 6 1 3 4 2 5 17

Sample Output:

  1. 7

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<map>
  3. using namespace std;
  4. map<int,int> Map;
  5. int main()
  6. {
  7. int i,j,n,m,k=0,t;
  8. scanf("%d",&n);
  9. for(i=0;i<n;i++)
  10. {
  11. scanf("%d",&m);
  12. if(m>k)
  13. k=m;
  14. Map[m]=1;
  15. }
  16. for(i=1;i<=k+1;i++)
  17. {
  18. if(Map.find(i)==Map.end())
  19. {
  20. printf("%d",i);
  21. break;
  22. }
  23. }
  24. return 0;
  25. }

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Find the Missing Number

    方法一: 数学方法,先找到最大的值,需要比较最大的值和array size, 要是比array size小, 说明最大值missing。 然后用等差数列公式求得如果不缺失值的和