题目:输入一个字符串,打印出该字符串中字符的所有排列。
例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。怎么弄?

解决方案 »

  1.   


                string s1 = Console.ReadLine();
                char[] s = s1.ToCharArray();
                for (int i = 0; i < s.Length; i++)
                {
                    for (int j = 0; j < s.Length; j++)
                    {
                        for (int k = 0; k < s.Length; k++)
                        {
                            if (s[i] != s[j] && s[j] != s[k] && s[i] != s[k])
                                Console.WriteLine(s[i].ToString() + s[j].ToString() + s[k].ToString());
                        }
                    }
                }
                Console.ReadKey();
      

  2.   

    using System.Collections.Generic;public List<string> GetFullArray(string str)
            {
                List<string> list = new List<string>();
                if (str.Length < 2)
                {
                    list = new List<string>();
                    list.Add(str);
                    return list;
                }
                for (int i = 0; i < str.Length; i++)
                {
                    List<string> list2 = new List<string>();
                    list2 = GetFullArray(str.Remove(i, 1));                list = new List<string>();
                    foreach (string item in list2)
                    {
                        for (int j = 0; j < item.Length + 1; j++)
                        {
                            list.Add(item.Insert(j, str[i].ToString()));
                        }
                    }
                }
                return list;
            }//测试
    protected void Button1_Click(object sender, EventArgs e)
            {
                List<string> aa = GetFullArray("abc");
                string bb = "";
                for (int i = 0; i < aa.Count; i++)
                {
                    bb += aa[i].ToString() + "  ";
                }
                Response.Write(bb);
            }
      

  3.   


                string a = "cdab";
                char[] ia = a.ToCharArray();
                ia = ia.OrderBy(n => n).ToArray();
                foreach (char c in ia)
                {
                    Console.WriteLine(c);
                }这个好象成的吧.
      

  4.   

    using System;
    using System.Collections;namespace AllCombinations
    {
     class GenerateWords
     {
      public static void Main() 
      {
       ArrayList list= GetString("abc");
       Console.WriteLine( "Count = " + res.Count);
       for(int i=0;i<list.Count;i++)
       {
        Console.WriteLine(list[i]);
       }
      }
      public static ArrayList GetString(string str)
      {
       ArrayList result = new ArrayList();
       result.Add(str[word.Length-1]);
       if(str.Length <= 1)
        return result;
       for(int i=str.Length -2 ;i>=0;i--)
       {
        int count = result.Count;
        for(int j=0;j<count;j++)
        {
         result.Add(str[i].ToString() + result[j].ToString());
        }
        result.Add(str[i].ToString());
       }
       return result;
      }
     }
    }
      

  5.   

    如果每个字符都是不同的话,思路还是比较简单的:
    字符串长度为N的,
    从N个字符中取一个,放在队伍的第一位,有N种取法;
    接下来就是(N-1)个字符中取一个,放在队伍的第二位,有(N-1)种取法;
    ...
    最后只剩下一个字符了,只好把它放在队伍的未尾,只有唯一的一种取法;
    所以结果应为: N*(N-1)*(N-2)*...*1 , 就是从1连续乘到N,应该就是N的阶乘种结果;最笨的方法,就是按照分析的思路一步步来:
    c:
    =========================================
    void aaa(char * pstring);int main(int argc , char *argv[])
    {
        char * string = "abc"; // 准备好要排列的字符串;    aaa(string); // 输出结果;
    }void aaa(char *pstring)
    {
        if (strlen(pstring) == 0) // 如果是空字符串,说明无须排序,打印回车
        {
             printf("\n");
             return;
        }    char room[99];
        strcpy(room , pstring);    for (int i = 0 ; i < strlen(room) ; i ++)
        {
            printf("%c" , room[i]); // 取一个字符,打印出来
            char room2[99]; // 得到剩下的字符串
            strcpy(room2 , room);
            room2[i] = 0;
            if (i < strlen(room)-1) 
            {
                strcat(room2 , room2+i+1);
            }
            aaa(room2); // 将剩下的字符串再分析一遍
        }
    }
    =========================================
      

  6.   

    如果出现 "abcb",重复字符的话,7楼的代码就不行了
      

  7.   

    你在后面搞个.distinct().toarray();
    还是照样OK的.
      

  8.   

    http://www.cnblogs.com/chenping-987123/archive/2010/11/09/1872682.html