日期:2014-05-18  浏览次数:20653 次

向数组中插入200万个随机不重复的数字
向数组中插入200万个随机不重复的数字,然后输出。。 应该怎么做呢。。?

------解决方案--------------------
提供给你个思路。
200万的数组,插入不同的数,每次插入前判断是否重复,是不可能的了。
unsigned int 范围是 0~4294967295,总共4294967296个数字
把他平均分成200万份,每份有2147.45个数字。
就以2147个数字为一份,在每份中随机出一个数字,随机插入到200万个数组中的一个,插满为止
遍历输出。
------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random(Guid.NewGuid().GetHashCode());
            var result = Enumerable.Range(0, 2500000).Select(x => r.Next()).Distinct().Take(2000000).ToArray();
            Console.WriteLine("结果的前1000个:");
            for (int i = 0; i < 200; i++)
            {
                Console.WriteLine(string.Join("\t", result.Skip(i * 5).Take(5).Select(x => x.ToString().PadLeft(10, ' '))));
            }
        }
    }
}