国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > 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();

        }

    }

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台州市| 浦城县| 视频| 德格县| 德阳市| 英吉沙县| 哈密市| 三原县| 西藏| 海南省| 灌南县| 兰州市| 三河市| 桐庐县| 尉氏县| 平塘县| 永宁县| 长阳| 壶关县| 佛学| 富蕴县| 延庆县| 钦州市| 通化县| 峨眉山市| 泽库县| 靖安县| 通州区| 磐石市| 永宁县| 商南县| 鄱阳县| 固原市| 科技| 灵丘县| 英超| 五寨县| 绥中县| 大埔县| 府谷县| 汪清县|