我想从1到9生成5个数如:在123456789 中
生成
12345
12346
12347
23456
我想生成这样不重复的5个数字。一直生成完它,请问要怎么做到呢?

解决方案 »

  1.   

    建一个链表,把1-9放进去,取随机数m=rand(9-n),取出第m个节点内数,然后将这个节点从链表中删除。
      

  2.   

    建一个链表,把1-9放进去,取随机数m=rand(9-n),取出第m个节点内数,然后将这个节点从链表中删除。
      

  3.   

    for i:=12345 to 98765 do
     begin
     i:=i+1;
     s:=inttostr(i);
     for x:=1 to 4 do
       begin
        for y:=2 to 5 do
         begin
         if s[x]=s[y] then exit;
         end;
       end;
     end;
      

  4.   

    for i:=12345 to 98765 do 
      s := inttoStr(I);
      

  5.   

    for ia=1 to 9 do
    begin
      for ib=1 to 9 do
      begin
        if ib=ia then break;
        for ic=1 to 9 do
        begin
          if ic=ia or ic=ib then break;
          for id=1 to 9 do
          begin
            if id=ia or id=ib or id=ic then break;
            for ie=1 to 9 do
            begin
              if ie=id or ie=ic or ie=ib or ie=ia then break;
              m = ia*10000 + ib*1000 + ic*100 + id*10 + ie
            end;
          end;
        end;
      end;
    end;
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      t: Cardinal;
      ia,ib,ic,id,ie: integer;
      m: integer;
    begin
      t:= GetTickCount;
      for ia:=1 to 9 do begin
        for ib:=1 to 9 do begin
          if ib=ia then break;
          for ic:=1 to 9 do begin
            if (ic=ia) or (ic=ib) then break;
            for id:=1 to 9 do begin
              if (id=ia) or (id=ib) or (id=ic) then break;
              for ie:=1 to 9 do begin
                if (ie=id) or (ie=ic) or (ie=ib) or (ie=ia) then break;
                memo1.Lines.Add( inttostr( ia*10000 + ib*1000 + ic*100 + id*10 + ie));
              end;
            end;
          end;
        end;
      end;
      ShowMessage(IntToStr(GetTickCount - n1)); //32 
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      t:   Cardinal;
      i,x,y: integer;
      s: string;
      b: boolean;
    begin
      t := GetTickCount;
      for i:=12345 to 98765 do begin
        s:=inttostr(i);
        b:=true;
        for x:=1 to 4 do begin
          for y:=2 to 5 do begin
            if s[x]=s[y] then begin
              b:=false;
              break;
            end;
          end;
          if not b then break;
        end;
        if not b then memo2.Lines.Add(s);
      end;
      ShowMessage(IntToStr(GetTickCount - n1)); //27203
    end;第一个的效率应该可以了吧
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      MySet: TMyInt;
      curNum,i,j,MyRes:integer;
    begin
      MyRes := 0;
      j := 0;
      Edit1.Text := '';
      while j < 5 do
      begin
        curNum := random(10);
        if curNum>0 then
        begin
          MySet := [curNum];
          i := 0;
          MyRes := curNum;
          while i < 4 do
          begin
            curNum := random(10);
            if (curNum>0) and not (curNum in MySet) then
            begin
              MySet := MySet + [curNum];
              MyRes := MyRes * 10 + curNum;
              inc(i);
            end;
          end;
          inc(j);
          Edit1.Text := Edit1.Text + ','+ IntToStr(MyRes);
        end;
      end;
    end;
    最后应该对生成的这五个数是否有重复的再做一次测试,我这里没做
      

  8.   

    type
      TMyInt = set of 1..9;
    这个别忘了
      

  9.   

    function CombinationCollocate( // ÅÅÁÐ×éºÏ
      mStrings: TStrings; // Êä³öÓÃ
      mStr: string; // ÐèÅÅÁеÄ×Ö·û´®
      mLen: Integer // ÅÅÁеij¤¶È
    ): Boolean;
      procedure fCombinationCollocate(
        mSubStr: string;
        mPath: string
      );
      var
        I: Integer;
        S: string;
      begin
        if Length(mPath) >= mLen then
          mStrings.Add(mPath)
        else for I := 1 to Length(mSubStr) do
        begin
          S := mSubStr;
          Delete(S, I, 1);
          fCombinationCollocate(S, mPath + mSubStr[I]);
        end;
      end;
    begin
      Result := False;
      if not Assigned(mStrings) then Exit;
      mStrings.BeginUpdate;
      try
        mStrings.Clear;
        fCombinationCollocate(mStr, '');
      finally
        mStrings.EndUpdate;
      end;
      Result := True;
    end; { CombinationCollocate }procedure TForm1.Button1Click(Sender: TObject);
    begin
      CombinationCollocate(Memo1.Lines, '123456789', 5);
    end;
    12345
    12346
    12347
    12348
    12349
    12354
    12356
    12357
    12358
    12359
    12364
    12365
    12367
    12368
    12369
    12374
    12375
    12376
    12378
    12379
    12384
    12385
    12386
    12387
    12389
    12394
    12395
    12396
    12397
    12398
    12435
    12436
    12437
    12438
    12439
    12453
    ....
    98724
    98725
    98726
    98731
    98732
    98734
    98735
    98736
    98741
    98742
    98743
    98745
    98746
    98751
    98752
    98753
    98754
    98756
    98761
    98762
    98763
    98764
    98765
      

  10.   

    function Combination(mStrings: TStrings; mStr: string;
      mLen: Integer): Boolean; { 组合 }
      procedure fCombination(mLeft, mRight: string);
      var
        I: Integer;
      begin
        if Length(mLeft) >= mLen then
          mStrings.Add(mLeft)
        else for I := 1 to Length(mRight) do
          fCombination(mLeft + Copy(mRight, I, 1), Copy(mRight, I + 1, MaxInt));
      end;
    begin
      Result := False;
      if not Assigned(mStrings) then Exit;
      mStrings.BeginUpdate;
      try
        mStrings.Clear;
        fCombination('', mStr);
      finally
        mStrings.EndUpdate;
      end;
      Result := True;
    end; { Combination }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Combination(Memo1.Lines, '123456789', 5);
    end;
    12345
    12346
    12347
    12348
    12349
    12356
    12357
    12358
    12359
    12367
    12368
    12369
    12378
    12379
    12389
    12456
    12457
    12458
    12459
    12467
    12468
    12469
    12478
    12479
    12489
    12567
    12568
    12569
    12578
    12579
    12589
    12678
    12679
    12689
    12789
    13456
    13457
    13458
    13459
    13467
    13468
    13469
    13478
    13479
    13489
    13567
    13568
    13569
    13578
    13579
    13589
    13678
    13679
    13689
    13789
    14567
    14568
    14569
    14578
    14579
    14589
    14678
    14679
    14689
    14789
    15678
    15679
    15689
    15789
    16789
    23456
    23457
    23458
    23459
    23467
    23468
    23469
    23478
    23479
    23489
    23567
    23568
    23569
    23578
    23579
    23589
    23678
    23679
    23689
    23789
    24567
    24568
    24569
    24578
    24579
    24589
    24678
    24679
    24689
    24789
    25678
    25679
    25689
    25789
    26789
    34567
    34568
    34569
    34578
    34579
    34589
    34678
    34679
    34689
    34789
    35678
    35679
    35689
    35789
    36789
    45678
    45679
    45689
    45789
    46789
    56789
      

  11.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      I,J : Integer;
      S : string;
    begin
      ListBox1.Items.BeginUpdate;
      for I := 12345 to 98765 do
      begin
    //转换成字符串
        S := IntToStr(I);
    //找里面是否有 0
        J := pos('0',S);
    //如果找不到 0,是需要的
        if J = 0 then
          ListBox1.Items.Add(IntToStr(I));
      end;
      ListBox1.Items.EndUpdate;
      //需要的总共有多少个
      showmessage(IntToStr(ListBox1.Items.Count));
    end;
      

  12.   

    常在CSDN上下东东,分不够,三分走人!这次的回复,希望不会太短
    ---------------------------------------------------------
    做什么搞的这么麻烦
    引用Math单元,使用RandamRange函数搞定
    自已看看这个函数
    DELPHI 3000多个函数很多东东他都给你考虑了,自己买本DELPHI函数的书,有空你就翻翻
    问三二700多个函数你也看看,
    没必要自己去写这样的方法嘛
    ---------------------------
    OVER