[原创]:排列(组合)算法的实现:

排列(组合)算法的实现

     /// <summary>
        /// 递归算法求排列
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="array"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        public static void GetPermutation<T>(T[] array, int startIndex, int endIndex)
        {
            if (startIndex == endIndex)
            {
                StringBuilder temp = new StringBuilder();

                foreach (var item in array)
                {
                    temp.Append(item.ToString());
                }

                Console.WriteLine(temp.ToString());
            }
            else
            {
                for (int i = startIndex; i <= endIndex; i++)
                {
                    Swap(ref array[startIndex], ref array[i]);
                    GetPermutation<T>(array, startIndex + 1, endIndex);
                    Swap(ref array[i], ref array[startIndex]);
                }
            }
        }

  

 组合的情况:

 public static void Combine(char[] array, bool[] b, int start, int end)
        {

            if (start <= end)
            {
                // 包含 array[start]
                b[start] = true;
                Combine(array, b, start + 1, end);

                // 不包含 array[start]
                b[start] = false;
                Combine(array, b, start + 1, end);
            }
            else
            {
                for (int i = 0; i <= end; ++i)
                {
                    if (b[i])
                    {
                        Console.WriteLine(array[i]);
                    }
                }

                Console.WriteLine("-------------------------");
            }
        }

 

 

refer: 

http://www.cnblogs.com/snowdust/archive/2010/01/20/1652161.html

http://blog.csdn.net/lcl_data/article/details/5286847

http://yuyangc0008.blog.163.com/blog/static/54754220138492632191/

好文章:

http://www.cnblogs.com/luxiaoxun/archive/2012/08/08/2628153.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值