如:A,B,C(顺序),求不同的组合形式如下
A
B
C
AB
AC
BC
ABC
以上是三个字母的各种组合形式,那如果是N个如何用程序实现?

解决方案 »

  1.   


            static void Main(string[] args)
            {
                string[] arr = new[] { "A", "B", "C", "D", "E" };
                GetCombination(arr);
            }        static void GetCombination(string[] nums)
            {
                double count = Math.Pow(2, nums.Length);
                for (int i = 1; i <= count - 1; i++)
                {
                    string str = Convert.ToString(i, 2).PadLeft(nums.Length, '0');
                    for (int j = 0; j < str.Length; j++)
                    {
                        if (str[j] == '1')
                        {
                            Console.Write(nums[j]);
                        }
                    }
                    Console.WriteLine();
                }
            }
    /*
    输出:
    E
    D
    DE
    C
    CE
    CD
    CDE
    B
    BE
    BD
    BDE
    BC
    BCE
    BCD
    BCDE
    A
    AE
    AD
    ADE
    AC
    ACE
    ACD
    ACDE
    AB
    ABE
    ABD
    ABDE
    ABC
    ABCE
    ABCD
    ABCDE
    */
    http://topic.csdn.net/u/20090217/21/f41ed9f6-f929-451c-a5c9-80d2e408422a.html?70689
      

  2.   

    随便写了一个,有点啰嗦,自己改进吧using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace Test_CSharp_Console
    {
        class Program
        {
            static char[] c = { 'A', 'B', 'C' };
            static void Main(string[] args)
            {            ShowArr(1);        }        static void ShowArr(int i)
            {
                
                int k, m, n;
                for (k = 0; k < 3; k++)
                {
                    if (i == 1)
                    {
                        Console.WriteLine(c[k]);
                        continue;
                    }
                    for (m = 0; m < 3; m++)
                    {
                        if (i == 2)
                        {
                            Console.WriteLine(c[k].ToString() + c[m].ToString());
                            continue;
                        }
                        for (n = 0; n < 3; n++)
                        {
                            if (i == 3)
                            {
                                Console.WriteLine(c[k].ToString() + c[m].ToString() + c[n].ToString());
                                continue;
                            }
                        }
                    }               
                    
                }
                i++;
                if (i > 3) return;
                ShowArr(i);
            }
        }
    }
      

  3.   

    返回结果如下A
    B
    C
    AA
    AB
    AC
    BA
    BB
    BC
    CA
    CB
    CC
    AAA
    AAB
    AAC
    ABA
    ABB
    ABC
    ACA
    ACB
    ACC
    BAA
    BAB
    BAC
    BBA
    BBB
    BBC
    BCA
    BCB
    BCC
    CAA
    CAB
    CAC
    CBA
    CBB
    CBC
    CCA
    CCB
    CCC
      

  4.   

    用迭代实现的,思想其实很简单
    你如果得到了ABC的所有组合,再得到ABCD的组合就很简单了,将D添加到ABC所有可能的组合里面,并添加一个单独的D
            static void Main(string[] args)
            {
                string str ="ABCD";
                List<char> input = new List<char>(str.ToCharArray());
                List<string> result = new List<string>();
                GetAllCombination(input, result);            foreach (string s in result)
                    Console.WriteLine(s);
            }        static void GetAllCombination(List<char> elements, List<string> combinations)
            {
                if (elements.Count == 0) return;
                char lastChar = elements[elements.Count - 1];
                elements.RemoveAt(elements.Count - 1);
                GetAllCombination(elements, combinations);
                List<string> copy = new List<string>(combinations.ToArray());
                foreach (string str in copy)
                    combinations.Add(str + lastChar);
                combinations.Add(lastChar.ToString());
            }