Java面试题:1100之间的数,被放在数组a[99]中,有一个数没有包含在其中,用java代码找出这个数

  1. import java.util.Random;   
  2.   
  3. /**  
  4.  * 查找缺失的一个数字  
  5.  * @author 老紫竹 java2000.net  
  6.  *  
  7.  */  
  8. public class Test {   
  9.   public static void main(String args[]) {   
  10.     int total = 0;   
  11.     int[] a = new int[100];   
  12.   
  13.     // 生成一个缺失的数组   
  14.     Random r = new Random();   
  15.     int lost = r.nextInt(99) + 1;   
  16.     for (int i = 1; i < lost; i++) {   
  17.       a[i - 1] = i;   
  18.     }   
  19.     for (int i = lost; i <= 99; i++) {   
  20.       a[i - 1] = i + 1;   
  21.     }   
  22.   
  23.     for (int i = 0; i < 99; i++) {   
  24.       total += a[i];   
  25.     }   
  26.     System.out.println("不包含的那个数是" + (5050 - total) + "/" + lost);   
  27.   }   
  28. }  


这个题网上很多地方可以找到答案,这里写的是一个片段,就是把这99个数加起来,因为1到100的和是5050,所以用5050减99个数的和就是不包含的数字。
原文地址:https://www.cnblogs.com/encounter/p/2189073.html