码迷,mamicode.com
首页 > 编程语言 > 详细

在数组中随机插入数字且不重复

时间:2016-10-23 23:30:31      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:lis   justify   readline   next   ext   and   dom   random   不重复   

产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。(两种方法)

1,

     List<int> myList = new List<int>();

            Random ran = new Random();

            while (myList.Count<100)

            {

                int num = ran.Next(1, 101);

                if (!myList.Contains(num))

                {

                    myList.Add(num);

                }

            }

            foreach (int item in myList)

            {

                Console.WriteLine(item);

            }

            Console.WriteLine(myList.Count);

            Console.ReadLine();

   2,

       HashSet<int> myList = new HashSet<int>();

            Random ran = new Random();

            while (myList.Count<100)

            {

                int num = ran.Next(1, 101);

                myList.Add(num);

            }

            foreach (int item in myList)

            {

                Console.WriteLine(item);

            }

            Console.WriteLine(myList.Count);

            Console.ReadLine();

在数组中随机插入数字且不重复

标签:lis   justify   readline   next   ext   and   dom   random   不重复   

原文地址:http://www.cnblogs.com/wangxlei/p/5991325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!