c#全排列 和 组合算法

参考:http://topic.csdn.net/u/20090220/23/e2d130d9-d7d4-4520-bec7-e78ae6ca9aff.html

http://topic.csdn.net/u/20091223/09/b841653f-5955-4708-b6a7-9b3b1f8c9f88.html?41398

 

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Collections;

using System.IO;

using System.Xml;

 

namespace CSharpTest

{

    class B

    {

        static void Main(string[] args)

        {

            List<string> list = new List<string>();

            for (int i = 0; i < 6; i++)

            {

                list.Add(i.ToString());

            }

 

            C(list, 3);

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

            A(list, 0, 4);

 

        }

 

        /// <summary>

        /// 对数组进行组合操作,选取selectCount个元素进行组合

        /// </summary>

        /// <param name="lsArray">即将进行组合操作的数组</param>

        /// <param name="selectCount">选取的元素的个数</param>

        static void C(List<string> lsArray, int selectCount)

        {

            int totolcount = lsArray.Count;

            int[] currectselect = new int[selectCount];

            int last = selectCount - 1;

 

            for (int i = 0; i < selectCount; i++)

            {

                currectselect[i] = i;

            }

 

            while (true)

            {

                for (int i = 0; i < selectCount; i++)

                {

                    Console.Write(" {0} ", lsArray[currectselect[i]]);

                }

                Console.WriteLine();

 

                if (currectselect[last] < totolcount - 1)

                {

                    currectselect[last]++;

                }

                else

                {

                    int pos = last;

                    while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)

                    {

                        pos--;

                    }

                    if (pos == 0) return;

                    currectselect[pos - 1]++;

                    for (int i = pos; i < selectCount; i++)

                    {

                        currectselect[i] = currectselect[i - 1] + 1;

                    }

                }

            }

        }

 

        /// <summary>

        /// 对数组进行全排列

        /// </summary>

        /// <param name="lsArray">要进行全排列的数组</param>

        /// <param name="begin">进行全排列的开始下标</param>

        /// <param name="end">进行全排列的结束下标</param>

        static void A(List<string> lsArray, int begin, int end)

        {

            if (begin == end)

            {

                for (int i = 0; i <= end; i++)

                    Console.Write(" {0} ", lsArray[i]);

                Console.WriteLine();

            }

            for (int i = begin; i <= end; i++)

            {

                Swap(lsArray, begin, i);

                A(lsArray, begin + 1, end);

                Swap(lsArray, begin, i);

            }

        }

 

        /// <summary>

        /// 交换数组中的下标为x,y的值

        /// </summary>

        /// <param name="lsArray">该数组</param>

        /// <param name="x"></param>

        /// <param name="y"></param>

        static void Swap(List<string> lsArray, int x, int y)

        {

            string t = lsArray[x];

            lsArray[x] = lsArray[y];

            lsArray[y] = t;

        }

 

 

    }

}


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值