在1,2,3,4,5,6,7,8,9,10 十个数字如何组合出所有满足条件的3个数字,并且不重复条件:大于等于5的2个数字,小于5的1个在线等;

解决方案 »

  1.   


    procedure TForm1.Button2Click(Sender: TObject);
    var
        i,j,k:integer;
        a,b,c:string;
    begin
        for i:=0 to 4 do
            for j:=5 to 9 do
                for k:=j+1 to 9 do
                begin
                    a:=IntToStr(i);
                    b:=IntToStr(j);
                    c:=InttoStr(k);
                    Memo1.Lines.Add(a+b+c);
                    Memo1.Lines.Add(a+c+b);
                    Memo1.Lines.Add(b+a+c);
                    Memo1.Lines.Add(b+c+a);
                    Memo1.Lines.Add(c+a+b);
                    Memo1.Lines.Add(c+b+a);
                end;
    end;
      

  2.   

    //这是我挪用版内一位n人的,自己也在用;你可以必成你需要的情况
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, cxLookAndFeels, dxSkinsForm, cxControls, cxContainer,
      cxEdit, cxLabel, ExtCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        dxSkinController1: TdxSkinController;
        Panel1: TPanel;
        cxLabel1: TcxLabel;
        Edit2: TEdit;
        Edit1: TEdit;
        cxLabel2: TcxLabel;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      GenRandom: TGenRandom;
      i, j, n: Integer;
      S: string;
      //sARR : array[33..126] of string;    //所有可见字符
      sARR : array[1..62] of string;
    begin
      try
        n := strtoint(edit1.text);
      except
        showmessage('请输入整数值!');
        exit;
      end;
      GenRandom := TGenRandom.Create;
      try    //for i := low(sARR) to High(sARR) do
          //sARR[i] := char(i);
        j := 1;
        for i := 48 to 57 do
        begin
          sARR[j] := char(i);
          inc(j);
        end;
        j := 10;
        for i := 65 to 90 do
        begin
          sARR[j] := char(i);
          inc(j);
        end;
        j := 36;
        for i := 97 to 122 do
        begin
          sARR[j] := char(i);
          inc(j);
        end;
        for i := low(sARR) to High(sARR) do
          GenRandom.AddValue(sARR[i]);    S := '';    while GenRandom.Count > 0 do
        begin
          if S = '' then
            S := GenRandom.Next()
          else
            S := S + GenRandom.Next();
        end;
        edit2.text := copy(S,1,n);
      finally
        GenRandom.Free;
        edit2.SetFocus;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      close;
    end;end.
    ////////////////////////////////////////////////////////////////
    unit Unit2;interfaceuses
      SysUtils, Classes;type
      TGenRandom = class
      private
        FList: TList;
        function GetValue(Index: Integer): string;
        procedure SetValue(Index: Integer; const Value: string);
        function GetCount: Integer;
      public
        constructor Create;
        destructor Destroy; override;
        procedure AddValue(Value: string);
        procedure RemoveAll;    function RemoveAt(Index: Integer): string;
        function Next(): string; //在List里随机选一个数
        property subValue[Index: Integer]: string read GetValue write SetValue;
        property Count: Integer read GetCount;
      end;implementation{ TGenRandom }constructor TGenRandom.Create;
    begin
      Randomize;
      FList := TList.Create;
    end;destructor TGenRandom.Destroy;
    begin
      RemoveAll;
      FList.Free;
      inherited;
    end;procedure TGenRandom.AddValue(Value: string);
    //加一个参与随机挑选的数
    var
      PValue : PString;
    begin
      New(PValue);
      PValue^ := Value;
      FList.Add(PValue);
    end;procedure TGenRandom.RemoveAll;
    var
      i: Integer;
    begin
      for i := FList.Count - 1 downto 0 do
        RemoveAt(i);
    end;function TGenRandom.RemoveAt(Index: Integer): string;
    //移去第Index项,并返回第Index项
    var
      P: Pstring;
    begin
      P := FList.Items[Index];
      Result := P^;
      Dispose(P);
      FList.Delete(Index);
    end;function TGenRandom.GetValue(Index: Integer): string;
    begin
      Result := Pstring(FList.Items[Index])^;
    end;procedure TGenRandom.SetValue(Index: Integer; const Value: string);
    begin
      Pstring(FList.Items[Index])^ := Value;
    end;function TGenRandom.GetCount: Integer;
    begin
      Result := FList.Count;
    end;function TGenRandom.Next: string;
    //随机选一个值,并把它移出,防止下次选择时重复出现
    //选择的范围为FList里所有的值
    begin
      Result := RemoveAt(Random(Count));
    end;end.