随机生存30W组数 ,范围1-100. 每组长40个,并保证每组数最少有3个不同如:A: 1,2,3,....40
B: 4,5,6.... 43 
有效率高方法吗?

解决方案 »

  1.   

    // 随机生成3个元素的数组集合arraylist
    //随机生成长度37位的数组 
    //每生成一个判断:
    取一个arraylist中的数组 遍历三个元素 是否包含在这个37位数组里
    不包含则37+3=40;
    直到30W组生成完毕
      

  2.   

    random一条一条往数组里插吧 有没有高效的不清楚
      

  3.   


    ArrayList arraylisttemp1 = new ArrayList();
    ArrayList arraylisttemp2 = new ArrayList();
    Random random = new Random();
    int[] nums3 = new int[3];
    int[] nums2 = new int[37];
    int[] nums1 = new int[3];
    int[] nums4 = new int[40];
    for (int i = 1; i <= 100; i++)
    {
    for (int j = 1; j <= 100; j++)
    {
    if (i != j)
    {
    for (int k = 1; k <= 100; k++)
    {
    if (i != k && j != k)
    {
    nums1[0] = i;
    nums1[1] = j;
    nums1[2] = k; arraylisttemp1.Add(nums1);
    }
    }
    }
    }
    } for (int j = 0; j < 300000; j++)//需要300000组数组
    { for (int i = 0; i < 37; i++)//随机生成37个元素的数组
    {
    int a = random.Next(1, 101);
    nums2[i] = a;
    }
    arraylisttemp2.AddRange(nums2); for (int h = 0; h < arraylisttemp1.Count; h++)//在所有三个数不同的(arraylisttemp1)里面取一个随机数组
    {
    int b = random.Next(1, arraylisttemp1.Count);
    //if (!arraylisttemp1.Contains(arraylisttemp1[b]))
    //    {
    ArrayList temp = new ArrayList(arraylisttemp1.Count - 1);//存储中间量 for (int i = 0; i < arraylisttemp1.Count-1; i++)//在arraylisttemp1中删除已经取出的 那个三元素数组
    {
    if (i < b)
    {
    temp.Add(arraylisttemp1[i]);
    }
    else if (i >= b)
    {
    nums3 = ((int[])arraylisttemp1[b]);
    temp.Add(arraylisttemp1[i + 1]);
    arraylisttemp1 = temp; }
    }
    //}
    }
    arraylisttemp2.AddRange(nums3);
    nums4 =(int []) arraylisttemp2 .ToArray (typeof(int));
    arraylisttemp2.Clear();
    foreach (int a in nums4)
    {
    Console.Write(a+",");
    }

    Console.WriteLine();
    Console.WriteLine();
    }
      

  4.   

    List<Int32[]> array = new List<int[]>(300000);
                var temp=Enumerable.Range(1, 100);
                for (int i = 0; i < 300000; i++)
                {
                    array.Add(temp.OrderBy(item=>Guid.NewGuid()).Skip(60).ToArray());
                }
      

  5.   

    arraylist不好吧,用泛型高效些吧
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("top 10 of the 300k:");
                foreach (var item in foo().Take(10))
                { 
                    Console.WriteLine(string.Join(", ", item));
                }
            }        static IEnumerable<int[]> foo()
            {
                return Enumerable.Range(0, 300000).Select((x) =>
                {
                    IEnumerable<int> s = new int[] { };
                    var rnd = new Random(Guid.NewGuid().GetHashCode());
                    while (s.GroupBy(y => y).Count() < 3)
                    {
                        s = Enumerable.Range(0, 40).Select(z => rnd.Next(1, 101));
                    }
                    return s.ToArray();
                });
            }
        }
    }top 10 of the 300k:
    83, 14, 98, 31, 49, 7, 97, 19, 65, 61, 46, 19, 84, 21, 88, 42, 46, 82, 13, 49, 2
    0, 76, 100, 72, 58, 5, 27, 14, 99, 80, 70, 34, 71, 66, 30, 93, 88, 85, 32, 91
    99, 18, 82, 65, 25, 70, 54, 19, 26, 84, 44, 64, 92, 83, 19, 30, 23, 31, 27, 43,
    90, 17, 84, 93, 37, 76, 54, 19, 76, 12, 57, 21, 81, 12, 23, 35, 32, 70, 65, 98
    81, 76, 67, 81, 91, 61, 67, 36, 13, 81, 78, 81, 47, 45, 97, 80, 32, 78, 82, 76,
    65, 61, 77, 9, 23, 87, 42, 29, 44, 9, 44, 99, 42, 20, 22, 48, 57, 8, 8, 42
    46, 42, 43, 11, 87, 61, 48, 61, 98, 97, 2, 80, 32, 6, 37, 13, 8, 2, 19, 74, 100,
     90, 26, 51, 45, 69, 20, 85, 70, 37, 71, 29, 2, 88, 96, 33, 82, 96, 74, 91
    20, 71, 75, 3, 57, 16, 60, 98, 28, 50, 48, 82, 51, 25, 33, 98, 81, 32, 13, 59, 3
    3, 19, 9, 77, 43, 91, 69, 75, 12, 17, 32, 77, 13, 86, 78, 58, 18, 93, 77, 50
    67, 38, 76, 41, 9, 12, 50, 88, 8, 79, 53, 30, 54, 54, 68, 31, 73, 39, 99, 13, 30
    , 58, 42, 44, 69, 4, 18, 50, 95, 12, 81, 21, 17, 74, 40, 69, 96, 41, 91, 65
    19, 63, 65, 21, 81, 64, 55, 37, 15, 5, 83, 33, 52, 88, 62, 36, 73, 68, 41, 61, 2
    6, 55, 66, 36, 56, 29, 25, 10, 96, 81, 27, 31, 75, 65, 90, 33, 46, 6, 15, 20
    55, 34, 92, 69, 3, 4, 71, 19, 8, 33, 8, 60, 36, 48, 60, 57, 36, 62, 5, 6, 78, 60
    , 56, 31, 33, 41, 44, 15, 99, 20, 72, 55, 17, 19, 6, 79, 86, 53, 8, 73
    29, 97, 84, 3, 78, 19, 100, 73, 65, 76, 91, 36, 14, 9, 20, 20, 87, 49, 9, 73, 70
    , 51, 32, 47, 43, 9, 18, 74, 60, 33, 78, 95, 94, 34, 96, 71, 7, 79, 73, 99
    63, 25, 17, 57, 87, 14, 83, 100, 78, 32, 14, 76, 51, 75, 78, 43, 59, 94, 6, 43,
    60, 74, 97, 65, 35, 78, 51, 21, 81, 32, 63, 79, 82, 17, 46, 83, 93, 13, 53, 75
    Press any key to continue . . .
      

  7.   

    在Intel Pentium Dual Core E2200上运行,30万全部产生,时间小于10秒。