麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

C#中生成隨機不重復數列的算法

2023-05-16 12:36:01
字體:
來源:轉載
供稿:網友

給定一個正整數n,需要輸出一個長度為n的數組,數組元素是隨機數,范圍為0 – n-1,且元素不能重復。比如 n = 3 時,需要獲取一個長度為3的數組,元素范圍為0-2;簡單的理解就是生成一個無序的隨機數組。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace RandomNumber

{

    class Program

    {

        static void Main(string[] args)

        {

            //初始化一個數組,如果數組沒有賦值,默認是0

            //int[] arr = SolveProblemWayOne(5);

            //int[] arr = SolveProblemWaySecond(5);

            //int[] arr = SolveProblemWayThird(10);

            int[] arr = SolveProblemWayFour(5);

            for (int i = 0; i < arr.Length; i++)

            {

                Console.Write("{0,5}", arr[i].ToString());

            }

            Console.ReadKey();

        }

        /// <summary>

        /// 循環判斷隨機出來的數字是否在數組中

        /// </summary>

        /// <param name="total"></param>

        /// <returns></returns>

        public static int[] SolveProblemWayOne(int count)

        {

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

            Random random = new Random();

            for (int i = 0; i < count; i++)

            {

                int number = random.Next(1, count + 1);

                while (resultList.Contains(number))

                {

                    number = random.Next(1, count + 1);

                }

                resultList.Add(number);

            }

            return resultList.ToArray();

        }

        /// <summary>

        /// 按照順序生成一個數組

        /// </summary>

        /// <param name="total"></param>

        /// <returns></returns>

        public static int[] SolveProblemWaySecond(int count)

        {

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

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

            for (int i = 0; i < count; i++)

            {

                orignalList.Add(i);

            }

            int maxIndex = count;

            Random random = new Random();

            for (int i = 0; i < count; i++)

            {

                //隨機索引

                int index = random.Next(0, maxIndex);

                resultList.Add(orignalList[index]);

                orignalList.RemoveAt(index);

                maxIndex--;

            }

            return resultList.ToArray();

        }

        /// <summary>

        /// 不刪除數據,然后的問題就是給最后的東西賦值

        /// </summary>

        /// <param name="count"></param>

        /// <returns></returns>

        public static int[] SolveProblemWayThird(int count)

        {

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

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

            for (int i = 0; i < count; i++)

            {

                orignalList.Add(i);

            }

            int minIndex = 0;

            Random random = new Random();

            for (int i = 0; i < count; i++)

            {

                //隨機索引

                int index = random.Next(minIndex, count);

                resultList.Add(orignalList[index]);

                //交換,由于索引自減,不需要將隨機的值賦值到最后

                //int temp = orignalList[index];

                orignalList[index] = orignalList[minIndex];

                //orignalList[minIndex] = temp;

                minIndex++;

            }

            return resultList.ToArray();

        }

        /// <summary>

        /// 簡潔方式

        /// </summary>

        /// <param name="count"></param>

        /// <returns></returns>

        public static int[] SolveProblemWayFour(int count)

        {

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

            for (int i = 0; i < count; i++)

            {

                resultList.Add(i);

            }

            int minIndex = 0;

            Random random = new Random();

            for (int i = 0; i < count; i++)

            {

                //隨機索引

                int index = random.Next(minIndex, count);

                //頭部交換

                int temp = resultList[index];

                resultList[index] = resultList[minIndex];

                resultList[minIndex] = temp;

                minIndex++;

            }

            return resultList.ToArray();

        }

    }

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色视频a级毛片 | 欧美一级爱操视频 | 精品欧美一区二区精品久久 | 成人国产高清 | 999插插插| 亚洲啪| 亚洲91精品| 国产免费人做人爱午夜视频 | 久章草在线视频 | 毛片韩国 | 亚洲一区二区三区在线免费观看 | 91资源在线观看 | 国产理论视频在线观看 | 黄色毛片前黄 | a视频在线播放 | 久久91精品久久久久清纯 | 欧美成年私人网站 | 欧美亚洲黄色 | 成人三级视频网站 | 久久性生活免费视频 | 福利在线国产 | 污视频在线免费播放 | 亚洲婷婷日日综合婷婷噜噜噜 | 男女羞羞视频 | 国产亚洲精品综合一区 | 亚洲欧洲日产v特级毛片 | 黄色特级毛片 | 久久精品亚洲成在人线av网址 | h视频在线观看免费 | 久久蜜桃香蕉精品一区二区三区 | 91久久极品少妇韩国 | 九九热精品视频在线免费观看 | 片在线观看 | 最新中文字幕免费视频 | 国产99久久久国产精品下药 | 一级黄色免费观看视频 | av免费av| 特级黄色一级毛片 | 久久久久久久久久亚洲 | 又黄又爽又色无遮挡免费 | 密室逃脱第一季免费观看完整在线 |