zoukankan      html  css  js  c++  java
  • 已知Random.Next(1,10),产生1至100不重复的随机数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace www.cnblogs.com.rock_chen
    {
        class Program
        {
            /// <summary>
            /// 已知Random.Next(1,10),产生1至100不重复的随机数据
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                GenerateRandom generateRandom = new GenerateRandom();
                var result = generateRandom.Calc();
                Console.WriteLine(string.Join(",", result));
                Console.WriteLine();
                Console.WriteLine(string.Join(",", result.OrderBy(p => p).ToArray()));
                Console.Read();
            }
        }
    
        public class GenerateRandom
        {
            Random random = new Random();
    
            public int[] Calc()
            {
                int[] ary = new int[100];
                int count = 0;
                int currentIndex = 0;
                int k = 0;
                while (count < 100)
                {
                    var index = RandomInt();
                    if (ary[currentIndex] == 0)
                    {
                        ary[currentIndex] = index;
                        ary[index - 1] = currentIndex + 1;
                        currentIndex = index - 1;
                        count = 2;
                        continue;
                    }
                    if (ary[index - 1] == 0 && currentIndex != index - 1)
                    {
                        ary[index - 1] = ary[currentIndex];
                        ary[currentIndex] = index;
                        currentIndex = index - 1;
                        count++;
                    }
                    else
                    {
                        while (k < 100 && ary[k] > 0)
                        {
                            k++;
                        }
                        if (currentIndex != k && k < 100)
                        {
                            ary[k] = ary[currentIndex];
                            ary[currentIndex] = k + 1;
                            currentIndex = k;
                            count++;
                        }
                    }
                }
                return ary;
            }
    
            public int RandomInt()
            {
                return (random.Next(1, 10) - 1) * 10//10-80十位数
                    + random.Next(1, 10) + random.Next(1, 10) - 1 //1_17 
                    + (random.Next(1, 10) & 3); //0-3
            }
        }
    }
  • 相关阅读:
    多校第四场
    codechef 两题
    Topcoder 多校T-shirt场
    状态压缩DP
    LUCAS 定理
    HDU 1104 Remainder
    HDU4542 小明系列故事——未知剩余系
    Codeforces Round #256 (Div. 2)
    Codeforces Round #FF (Div. 2)
    2016年川师大软件工程学生博客打分
  • 原文地址:https://www.cnblogs.com/rock_chen/p/2450112.html
Copyright © 2011-2022 走看看