从1,2,3,...9的数字中,如何得到下面的排列组合(列数不相等,但各列只和等于排列的数字的个数,即等于9):
比如:三列,用;号隔开。1;4;4
2;5;2
3;3;3
...比如,四列,用;号隔开
1;6;1;1
3;3;2;1
2;2;3;2
...

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        RichEdit1: TRichEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function PaiLie(x:Integer;s:string):string;
    var
      i:Integer;
      str:string;
      F:TextFile;
    begin  if x=0 then
      begin
        AssignFile(f,'c:\temp.txt');
        Append(f);
        writeln(f,Copy(s,1,Length(s)-1));
        CloseFile(f);
        result:=Copy(s,1,Length(s)-1);
      end
      else if x=1 then
      begin
        AssignFile(f,'c:\temp.txt');
        Append(f);
        writeln(f,s+'1');
        CloseFile(f);    result:=s+'1';
      end
      else
      begin
        for i:=1 to x do
        begin
          str:=s+inttostr(i)+';';
          result:=PaiLie(x-i,str);
        end;
      end;end;procedure TForm1.Button1Click(Sender: TObject);
    var
      F:TextFile;
      s:string;
    begin
      AssignFile(F,'c:\temp.txt');
      ReWrite(f);
      CloseFile(f);  s:='';
      s:=PaiLie(9,s);  AssignFile(F,'c:\temp.txt');
      ReSet(f);  while not eof(f) do
      begin
        readln(f,s);
        RichEdit1.Lines.Add(s);
      end;
      CloseFile(f);end;end.
      

  2.   

    sjtuyjc(sjtuyjc),非常感谢你,你的答案是正确的,我先把这100分加给你。
    我的题出的不是很严密,应该是这样的,有几列就必须有几项,比如(1,2,3,4,5,6,7,8,9)排成4列中可能是:
    1;6;1;1
    3;3;2;1
    9;0;0;0;
    0;9;0;0
    ...中间要考虑0的因素。可以考虑先(1,2,3,4,5,6,7,8,9)变成(0,1,2,3,4,5,6,7,8,9)再往下考虑。这个该如何实现呢?多谢。
      

  3.   

    sjtuyjc(sjtuyjc) ,分现在还不能给你,因为给分后就结贴了,你帮我把这个问题搞定后,我可以多加100分结贴给你