从1至100个整数之间每次随机抽五个(不是一个),抽过的不出现;请高手提供算法,谢谢!!!

解决方案 »

  1.   


     ArrayList list = new ArrayList();
            Random random = new Random(unchecked((int)DateTime.Now.Ticks));
            while (list.Count < 5)
            {
                int rnd = random.Next(1, 100);
                if (!list.Contains(rnd))
                    list.Add(rnd);
            }
      

  2.   

    Random rd = new Random();
            ArrayList al = new ArrayList();
            while (al.Count < 5)
            {
                int i = rd.Next(1, 100);
                if (!al.Contains(i))
                {
                    al.Add(i);
                }
            }
            //取al
      

  3.   

    如果不借助arrayList 数据类型,还有其它的算法吗?
      

  4.   


                List<int> list = new List<int>();
                for (int i = 0; i < 10; i++)//抽10次
                {
                    Random r = new Random(Environment.TickCount);
                    for (int j = 0; j < 5; j++)
                    {
                        int k = r.Next(1, 101);
                        while (list.Contains(k))
                            k = r.Next(1, 101);
                        list.Add(k);
                    }
                }
                for (int i = 0; i < 50; i += 5)
                    Console.WriteLine("第{0}次抽奖结果:{1},{2},{3},{4},{5}", i / 5 + 1, list[i], list[i + 1], list[i + 2], list[i + 3], list[i + 4]);
      

  5.   

    int[] arr = new int[100];
    int count = 5;
    for (int i = 0; i < 100; i++) arr[i] = i;
    Random random = new Random();
    for (int i = 0; i < count; i++)
    {
        int j = random.Next(arr.Length - i);
        Console.WriteLine(arr[j]); // 输出    int t = arr[j]; // 将抽中的数和最后的数交换
        arr[j] = arr[arr.Length - i - 1];
        arr[arr.Length - i - 1] = t;
    }
    蹭点分。
      

  6.   

    不借助 list 或 arraylist等...数据类型
      

  7.   

    int[] ints = new int[5];
            int cnum = 0;
            Random rd = new Random();
            while (cnum < 5)
            {
                int num = rd.Next(1, 100);
                bool isexist = false;
                for (int i = 0; i < 5; i++)
                {
                    if (ints[i] == num)
                    {
                        isexist = true;
                        break;
                    }
                }
                if (!isexist)
                {
                    ints[cnum] = num;
                    cnum++;
                }
            }
            //取ints
      

  8.   


    int[] list = new int[5];
            bool ret;
            for (int i = 0; i < 5; i++)
            {
                ret = true;
                int rnd = random.Next(1, 100);
                for (int j = 0; j < i; j++)
                {
                    if (list[j] == rnd)
                    {
                        ret = false;
                        break;
                    }
                }
                if (ret)
                    list[i] = rnd;
            }
      

  9.   


                int[] array = new int[5];
                Random r = new Random(Environment.TickCount);
                for (int i = 0; i < 5; i++)
                {
                    int t = r.Next(1, 101);
                    while (Array.IndexOf(array, t) > -1)
                        t = r.Next(1, 101);
                    array[i] = t;
                    Console.WriteLine(t);
                }
      

  10.   

    c#的数组和list , arraylist一样不纯净的。已经实现ICollection,IEnumerable,IList这类接口了,干脆你就说用c实现好了。