我想问一个排列方面的问题,是这样的:假如有一个数组int[] temp={1,2,3,4,5,6,7},如果要用循环来排列成如下形式:123456,123457,123467,123567,134567,124567,234567.也就是彩票中的复式算法,请问要如何实现?数组temp是可变长的,算法应该适用变长的数组,不能用递归。谢谢了,在线等,很急

解决方案 »

  1.   

    自己搞定了,用的办法比较原始,代码如下,谢谢各位了。        private static void show() 
            {
                //int[] ary = { 1,2,3,4,5,6,7,8,9,10,11, 12, 13, 14, 15, 16, 17,18,19,20,21,22,23,24,25 };
                int[] ary = { 1, 2, 3, 4, 5, 6, 7};
                int count = 0;
                string t = "";            for (int a = 0; a <= ary.Length-6; a++)
                {
                    for (int b = a; b < ary.Length-5; b++)
                    {
                        for (int c = b; c < ary.Length-4; c++)
                        {
                            for (int d = c; d < ary.Length-3; d++)
                            {
                                for (int e = d; e < ary.Length-2; e++)
                                {
                                    for (int f = e; f < ary.Length-1; f++)
                                    {
                                        bool bol = true;
                                        int[] itemp = { ary[a], ary[b + 1], ary[c + 1], ary[d + 1], ary[e + 1], ary[f + 1] };
                                        for (int z = 0; z < itemp.Length - 1; z++)
                                        {
                                            if (itemp[z] == itemp[z + 1])
                                            {
                                                bol = false;
                                                break;
                                            }
                                        }
                                        if (bol)
                                        {
                                            count++;
                                            t = ary[a] + "," + ary[b + 1] + "," + ary[c + 1] + "," + ary[d + 1] + "," + ary[e + 1] + "," + ary[f + 1];
                                            Console.WriteLine(t);
                                            Console.WriteLine("");
                                        }                                 
                                    }
                                }
                            }
                        }
                    }
                }
                Console.WriteLine("count="+count);
                Console.Read();
            }