求字符的组合的算法,原题目如下:Problem: Use your most hands-on programming language to implement a function that prints all possible combinations of the characters in a string. These combinations range in length from one to the length of the string. Two combinations that differ only in ordering of the characters ARE the same combination.  In other words, “12” and “31” are different combinations from the input string “123”, but “21 is the same as “12”.For example, if we use “wxyz” as parameter for Combination(“wxyz”) the function will print outw
x
y
z
wx
wy
wz
xy
xz
yz
wxy
wxz
wyz
xyz
wxyz

解决方案 »

  1.   

    谁帮我调试下,我的c++出故障了,不能调试!!!!!!!!!!
    void getchar(char * p)
    {
    int m = strlen(p);
    int x = m;
    while(x--)
    {
    int n = m;
    int * b = new int [m];
    while(n--)
    {
    if(n<x)
    {
    b[n] = 1;
    }
    else
    {
    b[n] = 0;
    }
    }
    for(i = 0,n = 1;n<m;n++,i++)
    {
    if(b[n] && !b[n+1])
    {
    b[n] = 0;
    b[n+1] = 1;
    for(int j = 0;j< m;j++)
    {
    cout<<p[b[j]]);
    }
    cout<<endl; n = 0;
    i = 0;
    }
    else if(i == m)
    {
    break;
    }
    }
    }
    }
      

  2.   

    我想了一个不知道对不
    按字符拆分wxyz
    分出4个 w,x,y,z
    拿出第一个w 与后面与w,x,y,z进行组合(不与与自身有相同字符的组合,也就是w)组出 wx wy wz 
    拿出x 与wx wy wz比较 找出拥有其字符所有组合的字符(w x,不与其组合),得出xy,xz
    拿出y与wx wy wz xy,xz 比较…… yz然后拿出wx 与其以外的字符组合 wxy wxz
    wy,方法同上与 wxy wxz 比较 组成wyz
    wz 与wxy wxz wyz 发现拥有其组合包含了所有字符  不再组合
    ……
      

  3.   

    我觉得应该是两个for循环,效率可能不高,仅仅是通用想法。
      

  4.   

    处理重复的字符不在本命题内讨论,你只管当作没有重复的来做
    ----
    写好才看到这个-_-
    用Pascal写了一下,代码如下,只做过简单的测试:program CharacterCombine;{$APPTYPE CONSOLE}var
      Chars : array[1..108] of char;
      Occurs : array[1..108] of integer;
      MaxCharCount : integer;procedure AddChar(chr : Char);
    var
      i : integer;
      exists : boolean;
    begin
      exists := false;
      for i := 1 to MaxCharCount do
      begin
        if Chars[i] = chr then
        begin
          Inc(Occurs[i]);
          exists := true;
          break;
        end;
      end;  if not exists then
      begin
        Inc(MaxCharCount);
        Chars[MaxCharCount] := chr;
        Occurs[MaxCharCount] := 1;
      end;
    end;procedure MakeOutput(charCount, currentPos, currentCharUsed : integer; currentOutputString : string);
    var
      i : integer;
      outputLen : integer;
    begin
      outputLen := Length(currentOutputString);  if outputLen = charCount then
      begin
        WriteLn(currentOutputString);
        exit;
      end;  if currentCharUsed < Occurs[currentPos] then
      begin
        MakeOutput(charCount, currentPos, currentCharUsed + 1, currentOutputString + Chars[currentPos]);
      end;  for i := currentPos + 1 to MaxCharCount do
      begin
        MakeOutput(charCount, i, 1, currentOutputString + Chars[i]);
      end;
    end;var
      inputString : string;
      inputLen : integer;
      i : integer;
    begin
      ReadLn(inputString);
      while inputString <> ''do
      begin
        inputLen := Length(inputString);    MaxCharCount := 0;
        for i := 1 to inputLen do
        begin
          AddChar(inputString[i]);
        end;    for i := 1 to inputLen do
        begin
          MakeOutput(i, 1, 0, '');
        end;    ReadLn(inputString);
      end;
    end.
      

  5.   

    using System;namespace ConsoleApplication2
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: 在此处添加代码以启动应用程序
    //
    string star=Console.ReadLine();
    // SortAll(star);
    // Console.ReadLine();
    CombAll(star);
    SortAll(star);
    Console.ReadLine();
    }
    #region  全排列
    static void SortAll(string star)
    {
    string intStar="|";
    for(int i=0;i<star.Length ;i++)
    {
    intStar+=i.ToString()+"|";
    }
    Sort(star,intStar,"|");
    } static void Sort(string star,string intStar,string StrIn)
    {
    string result="|";
    int i=star.Length ;
    if(StrIn.Length ==intStar.Length )
    {
    string[] temp=StrIn.Split('|');
    string StrOut="";
    for(int k=1;k<star.Length+1 ;k++)
    {
    int t=Convert.ToInt32(temp[k]);
    StrOut+=star.Substring(t,1);
    }
    Console.WriteLine(StrOut);
    }
    else
    {
    #region 开始循环取值
    for(int j=0;j<i;j++)
    {
    string mid="";
    string[] temp=intStar.Split('|');
    mid=temp[j+1];
    //mid=intStar.Substring(j,1);
    if(StrIn.IndexOf("|"+mid+"|")>=0)
    {
    }
    else
    {
    bool flag=true;
    //判断后面的字符是否用过
    //bool flag0=true; //mid对应的字符
    string mid0=star.Substring(j,1);
    //已经构成排列的数字字符串数组。
    string[] ok=StrIn.Split ('|');
    //依次取后面的字符
    for(int k=j+1;k<i;k++)
    {
    if(StrIn.IndexOf("|"+k.ToString()+"|")>=0)  //用过
    {
    }
    else  //没有用过
    {
    if(mid0==star.Substring(k,1))  //有相同的
    {
    flag=false;
    break;
    }
    else
    {
    flag=true;
    }
    }
    }
    if(flag)
    {
    result=StrIn+mid+"|";
    Sort(star,intStar,result);
    }
    }
    }  
    #endregion
    }
    }
    #endregion
    #region 全组合
    static void CombAll(string star)
    {
    string intStar="|";
    for(int i=0;i<star.Length ;i++)
    {
    intStar+=i.ToString()+"|";
    }
    for(int i=1;i<star.Length +1;i++)
    {
    Combination(star,"|","",i);
    }
    }
    /// <summary>
    /// 给定字符,给定长度(长度小于给定字符串的长度),列出所有组合
    /// 即给定的字符不能重复使用
    /// </summary>
    /// <param name="star">给定的初始字符串</param>
    /// <param name="INstartar">给定的初始字符串</param>
    /// <param name="l">组合后的字符串长度</param>
    /// 
    static void Combination(string star,string IntIn,string StrIn,int l)
    {
    if(StrIn.Length >=l)
    {
    if(StrIn.Length ==l)
    {
    Console.WriteLine(StrIn);
    }
    }
    else
    {
    #region 开始循环取值
    for(int j=0;j<star.Length ;j++)
    {
    string mid="";
    bool flag=true;
    mid=star.Substring(j,1);
    if(IntIn.IndexOf("|"+j.ToString()+"|")>=0)
    {
    }
    else
    {

    for(int k=j+1;k<star.Length ;k++)  //一次取后面的字符
    {
    if(IntIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
    {
    }
    else
    {
    if(mid==star.Substring(k,1))
    {
    flag=false;
    break;
    }
    else
    {
    flag=true;
    }
    }
    }   //一次取后面的字符

    if(flag)
    {
    bool lastflag=false;
    if(IntIn.Length >1)
    {
    int Intpre0=IntIn.LastIndexOf("|");
    string tempIntIn=IntIn.Remove(Intpre0,1);
    int Intpre1=tempIntIn.LastIndexOf("|");
    string Intpre=IntIn.Substring(Intpre1+1,Intpre0-Intpre1-1); int intPre=Convert.ToInt32(Intpre);
    if(intPre>j)
    {
    lastflag=true;
    }
    else
    {
    if(intPre<j)
    {
    lastflag=false;
    }
    else
    {
    if(mid==StrIn.Substring(StrIn.Length-1,1))
    {
    lastflag=true;
    }
    else
    {
    lastflag=false;
    }
    }
    }
    }
    else
    {
    lastflag=true;
    } if(lastflag)
    {
    string IntInTemp=IntIn+j.ToString()+"|";
    //IntIn+=j.ToString()+"|";
    string StrInTemp=StrIn+mid;
    //StrIn+=mid;
    Combination(star,IntInTemp,StrInTemp,l);
    }
    }
    }


    }
    #endregion
    }
    }
    #endregion
    /// <summary>
    /// 给定字符,给定长度(长度小于给定字符串的长度),列出所有排列
    /// 即给定的字符不能重复使用
    /// </summary>
    /// <param name="star"></param>
    /// <param name="IntIn"></param>
    /// <param name="StrIn"></param>
    /// <param name="l"></param>
    /// 

    static void Sort(string star,string IntIn,string StrIn,int l)
    {
    if(StrIn.Length >=l)
    {
    if(StrIn.Length ==l)
    {
    Console.WriteLine(StrIn);
    }
    }
    else
    {
    #region 开始循环取值
    for(int j=0;j<star.Length ;j++)
    {
    string mid="";
    bool flag=true;
    mid=star.Substring(j,1);
    if(IntIn.IndexOf("|"+j.ToString()+"|")>=0)
    {
    }
    else
    {

    for(int k=j+1;k<star.Length ;k++)  //一次取后面的字符
    {
    if(IntIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
    {
    }
    else
    {
    if(mid==star.Substring(k,1))
    {
    flag=false;
    break;
    }
    else
    {
    flag=true;
    }
    }
    }   //一次取后面的字符

    if(flag)
    {
    /*
    bool lastflag=false;
    if(IntIn.Length >1)
    {
    int Intpre0=IntIn.LastIndexOf("|");
    string tempIntIn=IntIn.Remove(Intpre0,1);
    int Intpre1=tempIntIn.LastIndexOf("|");
    string Intpre=IntIn.Substring(Intpre1+1,Intpre0-Intpre1-1); int intPre=Convert.ToInt32(Intpre);
    if(intPre>j)
    {
    lastflag=true;
    }
    else
    {
    if(intPre<j)
    {
    lastflag=false;
    }
    else
    {
    if(mid==StrIn.Substring(StrIn.Length-1,1))
    {
    lastflag=true;
    }
    else
    {
    lastflag=false;
    }
    }
    }
    }
    else
    {
    lastflag=true;
    }
    */
    //if(lastflag)
    //{
    string IntInTemp=IntIn+j.ToString()+"|";
    //IntIn+=j.ToString()+"|";
    string StrInTemp=StrIn+mid;
    //StrIn+=mid;
    Combination(star,IntInTemp,StrInTemp,l);
    //}
    }
    }


    }
    #endregion
    }
    }
    }
    }
      

  6.   

    var
      inputString : string;
      inputLen : integer;
      i : integer;
    从这一段代码开始相当于C中的main函数
      

  7.   

    公司电脑上只有Delphi,不然偶就用C++写了~
      

  8.   

    就是求组合嘛...应该不难撒,这种问题的应该价值是什么啊,还是纯粹为了玩..我想这样写可能容易些Amethod(params char[] cs)
    {
       ...
    }
      

  9.   

    to:  ProjectDD() ( )就是求组合嘛...应该不难撒,这种问题的应该价值是什么啊,还是纯粹为了玩..
    _______________________________________________________________________对你来说可能是玩,对我来说是饭碗的问题啊!!!!!!!!!!!!
      

  10.   

    这里有 C++ 实现的组合算法,效率应该还不错:)
    http://www.bxmy.org/article.html
      

  11.   

    TO foren_whb(丰云) 
    你的方法有问题,我帮你调了一下
    得出来的结果:xxwx
    xwxx
    xwxw
    xwwx其它没看
      

  12.   

    我刚看出来了几个错误,改了一下,有谁能帮我再调试下吗?void getchar(char * p)
    {
    int m = strlen(p);
    int x = m;
    while(x--)
    {
    int n = m;
    int * b = new int [m];
    while(n--)
    {
    if(n<x)
    {
    b[n] = 1;
    }
    else
    {
    b[n] = 0;
    }
    }
    for(i = 0,n = 1;n<m;n++,i++)
    {
    if(b[n] && !b[n+1])
    {
    b[n] = 0;
    b[n+1] = 1;
    for(int j = 0;j< m;j++)
    {
                                                  if(b[j])
                                                  {
              cout<<p[b[j]];
                                                  }
    }
    cout<<endl; n = 0;
    i = 0;
    }
    else if(i == m)
    {
    break;
    }
    }
    }
    }
      

  13.   

    to:  cxjddd(又是花开时) ( ) 信誉:100谢谢,你推荐的文章我刚看了一遍,写的很好。
    我上面的算法和文章中的方法有些接近,呵呵,
    可惜,现在不能调试,郁闷中。。
      

  14.   

    // 自我感觉我自己的不错。哈哈
    using System;
    using System.Collections;namespace AllCombinations
    {
    class GenerateWords
    {
    public static void Main() 
    {
    ArrayList res = Generate_Permutations("abcd");
    Console.WriteLine( "Count = " + res.Count);
    for(int i=0;i<res.Count;i++)
    {
    Console.WriteLine(res[i]);
    }
    }
    public static ArrayList Generate_Permutations(string word)
    {
    ArrayList result = new ArrayList();
    result.Add(word[word.Length-1]);
    if(word.Length <= 1)
    return result;
    for(int i=word.Length -2 ;i>=0;i--)
    {
    int count = result.Count;
    for(int j=0;j<count;j++)
    {
    result.Add(word[i].ToString() + result[j].ToString());
    }
    result.Add(word[i].ToString());
    }
    return result;
    }
    }
    }// The results is :
    Count = 15
    d
    cd
    c
    bd
    bcd
    bc
    b
    ad
    acd
    ac
    abd
    abcd
    abc
    ab
    a
      

  15.   

    这种问题应该是中学数学中学过的组合啊,查一下公式然后写成算法就可以了撒公式我记不到了,但帮你查了一下书,是这样的从n个不同元素中选择r个不同元素的组合数是C(n,r)=n!/r!(n-r)1例如C(25,2)=300有了这公式你的问题就解决了啊,只要调整调整来个循环把所有的1-n的组合数全部打出来就OK了.不过考虑到是组合问题所以需要在输入参数的时候有个不可输入重复字的检查.
      

  16.   

    对    diandian82(点点) ( ) 
    表示敬意!
    虽然用的c#,但代码非常精炼,执行效率也不差,过程也很巧妙!
      

  17.   

    procedure TForm2.Button1Click(Sender: TObject);
      function mm(n: integer): integer;
      var
        i: integer;
      begin
        result := 1;
        for i := 1 to n do result := result * 2; // 求2^n的函数
      end;
      procedure zh(n: integer);
      var
        i, j, n2, n1: integer;
        s: array of string;
      begin
        n1 := mm(n); //求2的n次方
        SetLength(s, n1 + 1);
        mmo1.Lines.Clear;
        for i := 1 to n do
        begin
          n2 := mm(i - 1);
          for j := 1 to n1 do
          begin
            if ((j div n2) mod 2) = 1 then s[j] := s[j] + IntToStr(i);
          end;
        end;
        for I := 1 to n1 do
          mmo1.Lines.Add(s[i]);
      end;
    var
      i: Integer;
    begin
      zh(4);
    end;