用递归实现排列组合取数(组合显示 例5选3等)

类别:编程语言 点击:0 评论:0 推荐:
例:
2选1
结果1
        2
5选2
结果1$2
         1$3
        1$4
        1$5
        2$3
        2$4
        2$5
        3$4
        3$5
        4$5
用BCB3实现
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    temp = "";
    count = 0;
    for(int i=0;i<100;i++)bt[i]="";
    Memo1->Lines->Clear();
    int x,y;
    x = Edit1->Text.ToIntDef(1);
    y = Edit2->Text.ToIntDef(1);
    Fun(x,y);
    Memo1->Lines->Add("总共"+AnsiString(count)+"条记录");
    j=0;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TForm1::Fun(int a,int b)
{
    if      (a==b)
    {
        for(int i=b;i>=1;i--)
            temp =temp + " $ " + (AnsiString)i;
        Memo1->Lines->Add(temp);
        temp ="";
        count++;
        return "";
    }
    else if (b==0)
    {
        Memo1->Lines->Add(temp);
        temp = "";
        count++;
        return "";
    }
    else
    {
            temp = Fun(a-1,b);
            bt[j]= " $ "+AnsiString(a);
            j++;
            int i=0;
            while(bt[i]!="")
            {
                temp = temp + bt[i];
                i++;
            }
            temp =  Fun(a-1,b-1) + temp ;
            bt[j]="";
            j--;
    }
}

本文地址:http://com.8s8s.com/it/it23594.htm