zoukankan      html  css  js  c++  java
  • 面试题1

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

     static void Main(string[] args)
            {
    
                //初始化集合List
                List<int> list = new List<int>();
                Random r = new Random();
                for (int i = 0; i < 98;)
                {
                    //随机生成1到100之间的整数
                    int num = r.Next(1,101);
                    //判断集合中是否有该整数,如果没有就加进去
                    if(!list.Contains(num))
                    {
                        list.Add(num);
                        i++;
                    }
                }
    
    
                //显示1到100中两个不在List中的数字
                for (int i = 1; i <= 100; i++)
                {
                    if (!list.Contains(i))
                    {
                        Console.WriteLine(i);
                    }
                }
            }
  • 相关阅读:
    hdoj:2033
    hdoj:2032
    hdoj:2031
    hdoj:2029
    hdoj:2028
    hdoj:2027
    hdoj:2024
    hdoj:2023
    hdoj:2022
    hdoj:题目分类
  • 原文地址:https://www.cnblogs.com/mdy41034264/p/3305583.html
Copyright © 2011-2022 走看看