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
  • 相关阅读:
    Spring简介和基础
    el表达式
    Spring MVC 文件上传下载
    el表达式的function标签
    JSTL核心标签库使用
    基于注解的SpringMVC
    new一个Object对象占用多少内存?
    leetcode-第k个排列(Java和c++版)
    使用maven构建web项目(简易版)
    leetcode-电话号码的字母组合
  • 原文地址:https://www.cnblogs.com/LifeForCode/p/3301459.html
Copyright © 2011-2022 走看看