loner_li 机试题 现有1~100共一百个自然数,已随机放入一个有98个元素的数组a[98]。

  要求写出一个尽量简单的方案,找出没有被放入数组的那2个数,并在屏幕上打印这2个数。注意:程序不用实现自然数随机放入数组的过程。

while (true)
            {
                int[] array=new int[98];
                Random r = new Random();
                List temp = new List();
                //随机产生98个 1-100之间的自然数
                while (temp.Count<98)
                {
                    int result = r.Next(1,101);
                    if (temp.Contains(result))
                    {
                        continue;
                    }
                    temp.Add(result);

                }
                //将98个数填充到数组中
                for (int i = 0; i < 98; i++)
                {
                    array[i]=temp[i];
                }
                ///*强烈推荐这种做法开始*/
                标识为1
                int[] arraytemp=new int[101];
                for (int i = 0; i < 98; i++)
                {
                    arraytemp[array[i]] = 1;//给98个自然数标识
                }
                for (int i = 1; i <= 100; i++)
                {
                    if (arraytemp[i]==1)
                    {
                        continue;
                    }
                    Console.Write(i);
                }
                    Console.ReadKey();
            }
          
        }

 

你可能感兴趣的:(C#,C#,机试题)