简体   繁体   English

C程序在一系列数字中找到缺失的整数

[英]C program to find missing integer in a sequence of numbers

You are given a sequence of n-1 distinct positive integers, all of which are less than or equal to a integer 'n'. 给出一系列n-1个不同的正整数,所有这些整数都小于或等于整数'n'。 You have to find the integer that is missing from the range [1,2,...,n]. 你必须找到范围[1,2,...,n]中缺少的整数。 Solve the question without using arrays. 在不使用数组的情况下解决问题。

Input Format: One line containing the integer 'n' where 2<=n<=10,000 First line is followed by a sequence of 'n-1' distinct positive integers. 输入格式:包含整数'n'的一行,其中2 <= n <= 10,000第一行后跟一系列'n-1'个不同的正整数。 Note that the sequence may not be in any particular order. 请注意,序列可能没有任何特定顺序。

I got code by using arrays 我通过使用数组获得了代码

#include<stdio.h>
int main()
{
 int i,j,n[9999],m,t;
 scanf("%d",&m);
 for(i=1;i<m;i++)
  {
   scanf("%d",&n[i]);
  }
 for(i=1;i<m;i++)
  {
   for(j=1;j<i;j++)
    {
      if(n[j]>n[j+1])
       {
         t=n[j];
         n[j]=n[j+1];
         n[j+1]=t;
        }
    }
   }
   for(i=2;i<m;i++)
    {
     if(n[i-1]!=n[i]-1)
       {
          printf("%d",n[i]-1);
          break;
       }
  }
 return(0);
 }

How can I do the same without using arrays? 如何在不使用数组的情况下执行相同操作?

The logic is simple. 逻辑很简单。 You just find the sum of the continuous numbers in series for a given n. 您只需找到给定n的连续数字的总和。

And, now add all those numbers provided in the question to find the sum exactly of the given numbers. 并且,现在添加问题中提供的所有数字,以找到给定数字的总和。

The difference is what you can say that difference between those 2 sums is the missing number. 区别在于你可以说这两个总和之间的差异是缺失的数字。

Ex :- Let's say, n = 6. 例如: - 比方说,n = 6。

So, you just find the sum of n consecutive integers starting from 1,...,6 is :- 6 * (6+1) / 2 = 21. Formula of sum of n consecutive integers starting from 1 is {n * (n+1)} / 2. 所以,你只能找到从1开始的n个连续整数之和,...,6是: - 6 *(6 + 1)/ 2 = 21.从1开始的n个连续整数之和的公式是{n *( n + 1)} / 2。

And, now find the sum of given n-1 numbers. 并且,现在找到给定n-1个数的总和。

Say, numbers given are 1,2,4,5,6. 比如,给出的数字是1,2,4,5,6。 Then their sum = 1 + 2 + 4 + 5 + 6 = 18. 然后他们的总和= 1 + 2 + 4 + 5 + 6 = 18。

Therefore, the missing number = sum of continuous n numbers - sum of given (n-1) numbers = 3. 因此,缺失数=连续n个数的和 - 给定(n-1)个数之和= 3。

Find the sum of the given integers. 找到给定整数的总和。 Subtract it from n(n+1)/2. 从n(n + 1)/ 2中减去它。

Explanation: The sum of the first n integers is n(n+1)/2. 说明:前n个整数的和为n(n + 1)/ 2。 Therefore the sum of the given integers + missing integer= n(n+1)/2. 因此,给定整数的总和+缺失整数= n(n + 1)/ 2。

Suppose n=10; 假设n = 10;

Then, 1+2+3+4+5+6+7+8+9+10=10*(10+1)/2==55 然后,1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 10 *(10 + 1)/ 2 == 55

If the given integers are- 1,2,3,4,5,6,7,8,10. 如果给定的整数是-1,2,3,4,5,6,7,8,10。

Then answer = 55 - (1+2+3+4+5+6+7+8+10)=9. 然后回答= 55 - (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 10)= 9。

To find the missing number in the array of 1 to n-1 then we have two methods one is sum formula and second one is use XOR method where both gives O(n) time complexity. 为了找到1到n-1数组中缺少的数字,我们有两个方法,一个是和公式,第二个是使用XOR方法,其中两个方法都给出O(n)时间复杂度。

Use Sum Formula 使用总和公式

1 Get the sum of numbers 1获取数字之和

   total = n*(n+1)/2

2 Subtract all the numbers from sum and you will get the missing number. 2从总和中减去所有数字,你将得到缺失的数字。

Use XOR Method 使用XOR方法

1 XOR all the array elements, let the result of XOR be X1. 1 XOR所有数组元素,让XOR的结果为X1。

2 XOR all numbers from 1 to n, let XOR be X2. 2 XOR从1到n的所有数字,让XOR为X2。

3 XOR of X1 and X2 gives the missing number. X1和X2的3 XOR给出缺失的数字。

'你可以使用sum方法作为数组中总数的总和,如总数组大小= 5,元素是1,2,4,5,6然后大小= 5然后使用此方法(n(n + 1))/ 2这里n = 5所以15通过计算再次得到数组1 + 2 + 4 + 5 + 6 = 18的元素之和因此18-15 = 3简单'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM