zoukankan      html  css  js  c++  java
  • 3.随机排序一维数组 int[]

                #region 3、随机排序一维数组 int[]
    
                #region 方法1
    
                int[] oldnum = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                int[] newnum = new int[oldnum.Length];
                Random ran = new Random();
                List<int> intlist = new List<int>();
    
                for (int i = 0; i < oldnum.Length; i++)
                {
                    int random = ran.Next(oldnum.Length - 1);
                    while (intlist.Contains(random))
                    {
                        random = ran.Next(oldnum.Length);
                    }
                    intlist.Add(random);
                }
    
    
                for (int j = 0; j < intlist.Count; j++)
                {
                    int a = intlist[j];
                    newnum[j] = oldnum[a];
                }
    
                for (int k = 0; k < newnum.Length; k++)
                {
                    Console.WriteLine(newnum[k].ToString() + ";");
                }
                #endregion
    
                #region 方法2 以前写的,小纪念下
    
                //3、随机排序一维数组  int[] 。
    
                int[] intArr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
                int[] intArr2 = new int[10];
    
                Console.WriteLine("排序前");
                for (int i = 0; i < intArr.Length; i++)
                {
                    Console.Write(intArr[i] + ";");
                }
                List<int> newlist = new List<int>();
                for (int i = 0; i < intArr.Length; i++)
                {
                    Random rans = new Random();
                    int random = rans.Next(0, 10);
                    if (newlist.Count == 10)
                    {
                        break;
                    }
                    if (!newlist.Contains(random))
                    {
                        newlist.Add(random);
                    }
                    else
                    {
                        i--;
    
                    }
    
                }
                Console.WriteLine();
                Console.WriteLine("排序后");
                for (int j = 0; j < intArr.Length; j++)
                {
                    intArr2[j] = intArr[newlist[j]];
    
                    //Console.Write(intArr[newlist[j]] + ";");
                }
    
                intArr = intArr2;
                for (int k = 0; k < intArr.Length; k++)
                {
                    Console.Write(intArr[k] + "");
                }
                #endregion
    
                #endregion
  • 相关阅读:
    Altera Coding Style 之多路选择器
    Altera Coding Style 之 时钟切换
    Altera Coding Style 之状态机
    Altera Coding Style 之 Latch
    Altera Coding Style 之 加法器
    JS代码小集合
    汉字转换字符C#(c shap) 代码
    php动态调用方法
    JavaScript 拖放效果
    C# 取得域名及目录地址
  • 原文地址:https://www.cnblogs.com/LifeForCode/p/3301459.html
Copyright © 2011-2022 走看看