有7个不同的数字,怎样输出N(N≤7)个不同组合的加法算式?一条式子中不允许同时出现2个一样的数字。如:不能出现A+A这样的式子。

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
    h,i,j,k,l,m,n:integer;
    begin
      for h:=1 to 7 do
      begin
        memo1.Lines.Add(inttostr(h));
        for i:=h+1 to 7 do
        begin
          memo1.Lines.Add(inttostr(h)+'+'+inttostr(i));
          for j:=i+1 to 7 do
          begin
            memo1.Lines.Add(inttostr(h)+'+'+inttostr(i)+'+'+inttostr(j));
            for k:=j+1 to 7 do
            begin
              memo1.Lines.Add(inttostr(h)+'+'+inttostr(i)+'+'+inttostr(j)+'+'+inttostr(k));
              for l:=k+1 to 7 do
              begin
                memo1.Lines.Add(inttostr(h)+'+'+inttostr(i)+'+'+inttostr(j)+'+'+inttostr(k)+'+'+inttostr(l));
                for m:=l+1 to 7 do
                  memo1.Lines.Add(inttostr(h)+'+'+inttostr(i)+'+'+inttostr(j)+'+'+inttostr(k)+'+'+inttostr(l)+'+'+inttostr(m));
              end;
            end;
          end;
        end;
      end;
    end;end.
      

  2.   

    TO:coffeesun(寰宇) ,我用你的这个方法试了一下,在我的P4(1.6)的机子上,速度很慢的。因为这7个数字是存在数据库中的。如果要求是从20个数字中选出7个,那不是更慢得象牛一样。还有什么方法可以优化的呢?
      

  3.   

    ??
    float a[7];
    a[i]....
    for(i=0;i<7;++i)
      a[i]+a[i+1]
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages,  Variants, Classes, Graphics, Controls, Forms,
      Dialogs,SysUtils,strutils, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      myarray:array[1..792]of string;  //保存结果
      index:integer;//记数器的功能
    implementation
    {$R *.dfm}
    procedure liechu(n:string);
    var   j,k:integer;
          v:char;
    begin
         for k:=1 to length(n)  do
         begin
           v:=n[k];
           j:=k;
           while(j<>1)and(n[j-1]<v) do
             begin
               n[j]:=n[j-1];
               j:=j-1;
             end;
           n[j]:=v;
         end;
      if not ansimatchstr(n,myarray) then
           begin
             index:=index+1;  //记录计数
             myarray[index]:=n;
           end;
    end;
    procedure list(Full: String; Result: String; Max: Integer);
    //Full:等待排列的总串;result:符合条件的串 MAX:总串的长度
    var
      I: Integer;
    begin
     if Length(Result) = Max then
       begin
         liechu(Result); //保存结果
         Exit;
       end;
      for I := 1 to Length(Full) do
      begin
        if Pos(Full[I], Result) = 0 then //如果子串中没有该元素
          list(Full, Result + Full[I], Max);
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I,j: Integer;
      S: String;
    begin
      index:=0;
      for J:=1 to 7 do
      begin
      index:=0;
      for i:=1 to 792 do
        myarray[i]:='';
      S := 'abcdefg';
      List(S, '', j);
      for i:=1 to index do
        memo1.Lines.Add(myarray[i]) ;
    end;
    end;
    end.
      

  5.   

    //我试用了,效果不错,还可以举一反三!!!
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button3: TButton;
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function getnum(m:integer;n:integer):integer;stdcall;
    var
      i,j,k:integer;
      b:array [0..100] of integer;
      s:string;
    begin
      k:=0;
      for i:=1 to n do b[i]:=i;
      b[0]:=-1;
      while b[0]<0 do
        begin
          s:='';
          j:=n;
          inc(k);
          while b[j]=m-n+j do j:=j-1;
          b[j]:=b[j]+1;
          for i:=j+1 to n do b[i]:=b[i-1]+1;
          for i:=1 to n do
          begin
            if i>1 then
              s:=s+' + '+inttostr(b[i])
            else
              s:=s+inttostr(b[i]);
          end;
          form1.memo1.Lines.Add(s);
        end;
        result:=k;//个数
    end;
    procedure TForm1.Button3Click(Sender: TObject);
    var i:integer;
        count:integer;
    begin
      count:=0;
      for i:=1 to 7 do
       count:=count+getnum(7,i);
      memo1.Lines.Add('总的个数为: '+inttostr(count)) ;
    end;end.
      

  6.   

    TO: xiangwangz,你的算法很不错的!非常值得大家学习学习!