zoukankan      html  css  js  c++  java
  • 在数组中随机插入数字且不重复

    产生一个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();

  • 相关阅读:
    自考毕业答辩总结
    【项目经验】navicat工具 SQLServer数据库迁移MySQL
    【项目经验】EasyUI Tree
    Django框架基础(一)
    前端相关内容补充
    web框架前戏---web框架的本质
    web框架前戏---基础简介及Django安装配置
    sqlAchemy前戏
    mysql基础(五)之pymysql
    mysql基础(四)之索引
  • 原文地址:https://www.cnblogs.com/wangxlei/p/5991325.html
Copyright © 2011-2022 走看看