现有1~100 共一百个自然数,已随机放入一个有98个元素的数组a[98].要求写出一个尽量简单的方案找出没有被放入数组的那2个数,并在屏幕上打印这2个数

 1  void test7()
 2         {
 3             try
 4             {
 5                 int[] a = new int[98];
 6                 int[] num = new int[100];
 7                 int[] num1 = new int[100];
 8                 int countnum = 0;
 9                 int counta = 0;
10                 int i;
11                 for (i = 0; i < num.Length; i++)
12                 {
13                     num[i] = i + 1;
14                     num1[i] = i + 1;//方法1用到
15                     countnum = num[i] + countnum;
16                 }
17                 //随机放入
18                 Random rand = new Random();
19                 int temp; 
20                 for (i = 0; i < a.Length; i++)
21                 {
22                     while (true)
23                     {
24                         temp = rand.Next(100);
25                         if (num[temp] != 0)
26                         {
27                             num[temp] = 0;
28                             a[i] = temp + 1;
29                             break;
30                         }
31                     }
32                 } 
33                 foreach (int aa in a)
34                 {
35                     counta = counta + aa;
36 
37                     Console.WriteLine(aa+";");
38                 }
39 
40                 #region 方法1 推荐,这才是插入完,再查找
41                 int diff = countnum - counta;
42                 List<int> list = num.Where(n => num.Contains(diff - n)).ToList<int>();//验证,num中只剩两个没变0的
43                 List<int> list1 = num1.Where(n => num1.Contains(diff - n) && !a.Contains(n)).ToList<int>();//查找,针对的是没有处理过得原数组
44                 Console.WriteLine("list.count:"+list.Count);
45                 Console.WriteLine("list1.count:" + list1.Count);
46                 foreach (int nn in list)
47                 {
48                     //if (!a.Contains(nn))
49                     //{
50                     Console.WriteLine("没有被放入数组的数(list):"); 
51                         Console.WriteLine(nn);
52                         //Console.WriteLine("bingo");
53                     //}
54                 }
55                 foreach (int nn in list1)
56                 { 
57                     //if (!a.Contains(nn))
58                     //{
59                     Console.WriteLine("没有被放入数组的数(list1):"); 
60                         Console.WriteLine(nn);
61                         //Console.WriteLine("bingo1");
62                     //}
63                 }
64                 #endregion
65 
66                 #region 方法2 太投机取巧了,其实是在随机插入的过程中把插入的全置为0,剩下的就是没插入的
67                 for (i = 0; i < a.Length; i++)
68                     if (num[a[i] - 1] != 0)
69                         num[a[i] - 1] = 0;
70                 //输出
71                 for (i = 0; i < a.Length; i++)
72                 {
73                     if (i % 10 == 0 && i > 0)
74                         System.Console.WriteLine();
75                     System.Console.Write("{0,4}", a[i]);
76                 }
77                 System.Console.WriteLine();
78                 System.Console.Write("没有被放入数组的2个数:");
79                 for (i = 0; i < num.Length; i++)
80                     if (num[i] != 0)
81                         System.Console.Write("{0,4}", num[i]);
82                 System.Console.WriteLine();
83                 #endregion
84             }
85             catch (Exception)
86             {
87 
88                 throw;
89             }
90         }

 

posted @ 2017-07-03 17:49  dyfisgod  阅读(2200)  评论(0编辑  收藏  举报