大家好,请大家帮忙一个问题,就是输出一个字符串的所有组合的问题。如给定"ABC"可以输出"ABC"/"ACB"/"BCA"/"BAC"/"CAB"/"CBA"/这六种组合,同样"ABCD"可以输出24种组合,不知如何来实现呢?刚才已经发了这个贴子的了,有人回复说讨论过了,我看了一下,直接就结贴给分了,那知细看的时候才知道不是自己想要的那个结果。请大家再看清楚一点:是一个字符串的所有组合。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Collections;namespace ConsoleApplication8
    {
        class Program
        {        public static void Main()
            {
                char[] number = new char[] { 'A', 'B', 'C' };
                Func(number, 0, number.Length - 1);
                Console.WriteLine(list.Count);            foreach (String s in list)
                {
                    Console.Write(s + " ");
                }
            }        public static List<string> list = new List<string>();
            public static void Func(char[] n, int beg, int end)
            {
                if (beg == end)
                {
                    string result = new string(n);              
                    list.Add(result);
                    return;
                }
                for (int i = beg; i <= end; i++)
                {
                    Swap(n, beg, i);
                    Func(n, beg + 1, end);
                    Swap(n, beg, i);
                }
            }        public static void Swap(char[] n, int a, int b)
            {
                char temp = n[a];
                n[a] = n[b];
                n[b] = temp;
            } 
            }}http://topic.csdn.net/u/20091223/09/b841653f-5955-4708-b6a7-9b3b1f8c9f88.html
      

  2.   

    全组合http://topic.csdn.net/u/20090217/21/f41ed9f6-f929-451c-a5c9-80d2e408422a.html