C#常见笔试题

1.产生20个不同的两位整数的随机数,并且对它们进行由小到大的排序。特别提醒:程序要自动生成20个不同的整数,而且这些整数必须是两位的,如:3不是两位整数,58是两位整数

View Code
            List<int> numbers= new List<int>();
            Random rnd = new Random();
            int i = 0;
            while (i < 20)
            {
                int s = rnd.Next(10, 100); //注意s的值大于等于10小于100
                if (!numbers.Contains(s))
                {
                    numbers.Add(s);
                    i++;
                }
            }
            foreach (int n in numbers)
            {
                Console.WriteLine(n);
            }

2.求质数

①.能被2,3,5,7整除的数都不是质数

View Code
            List<int> Primes = new List<int>();
            Primes.Add(2);
            Primes.Add(3);
            Primes.Add(5);
            Primes.Add(7);
            for (int a = 2; a < 100; a++)
            {
                if (a % 2 != 0 && a % 3 != 0 && a % 5 != 0 && a % 7 != 0)
                {
                    Primes.Add(a);
                }
            }

                foreach (int a in Primes)
                {
                    Console.WriteLine(a);
                }
            Console.ReadKey();
          
        }
原文地址:https://www.cnblogs.com/huangll/p/2751006.html