zoukankan      html  css  js  c++  java
  • pta 1144 The Missing Number

    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 (≤10​5​​). 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:

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

    Sample Output:

    7
    

    题目大意:给定n个数,找出这n个数里丢失的最小的正整数

    注意细节:

    (1)可能n个数全是负数,输出1

    (2)正整数要从1开始记录,数中可能存在重复的数

    (3)可能n个数都是连续的正整数,且从1开始,那么输出最大的数+1

    代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    int a[1000002];
    int main()
    {
    	int n;
    	while(cin >> n)
    	{
    		for(int i = 0;i < n;i ++)
    		{
    			cin >> a[i];
    		}
    		sort(a,a + n);
    		int i,fl = 0;
    		for(i = 0;i < n - 1;i ++)
    		{
    			if(a[i] > 0)
    			{
    				if(a[i] == 1)
    				fl = 1;
    				if(fl)
    				{
    					if(a[i] == a[i + 1] || a[i + 1] == a[i] + 1)
    					continue;
    					else
    					{
    						cout << a[i] + 1 << endl;
    						break;
    					}
    				}
    				else
    				{
    					cout << fl + 1 << endl;
    					break;
    				}
    			}
    		}
    		if(i == n - 1)
    		{
    			if(a[n - 1] > 0)
    			cout << a[n - 1] + 1 << endl;
    			else
    			cout << 1 << endl;
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    辣条君写爬虫1【贝壳房价爬取】
    如何在 Windows 上创建一个新的 GPG key
    Protocol Buffers 开发者指南
    Docsify 的边栏目录如何设置
    Docker 容器的网络
    Discourse 如何添加 Google Analytics GA4 代码
    Docker 引擎
    Docker 引擎
    Docker 引擎
    Docker 引擎概述
  • 原文地址:https://www.cnblogs.com/lu1nacy/p/10016628.html
Copyright © 2011-2022 走看看