現有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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM