Delphi 面试题 获取5位不重复的数字 数字在 1-8中

function getint(t: Integer): string;
var
  k: array of Byte;
  j, i,: integer;


  function isin(l: integer): Boolean;
  var
    i: Integer;
  begin
    result := False;
    for i := 0 to Length(k) do
    begin
      if l = k[i] then
      begin
        result := true;
        Break;
      end;
    end;
  end;


begin
  if t > 8 then
    Exit;
  case t of
    7:
      begin
        Result := '1234567';
        Exit;
      end;


    8:
      begin
        Result := '12345678';
        Exit;
      end

  end;

   randomize;

  SetLength(k, t);
  i := 0;
  while true do
  begin
    if i >= t then
      Break;
    j := Random(8);
    if (j = 0) or isin(j) then
    begin
      Continue;
    end;
      k[i] := j;
    Inc(i);
  end;
  for i := 0 to t - 1 do
    Result := Result + Format('%d', [k[i]]);
  SetLength(k, 0);

end;

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

加入字母数字生成订单号

function getint(t: Integer): string;
var
  k: array of Byte;
  j, i: integer;
  lchar: string;




  function isin(l: integer): Boolean;
  var
    i: Integer;
  begin
    result := False;
    for i := 0 to Length(k) do
    begin
      if l = k[i] then
      begin
        result := true;
        Break;
      end;
    end;
  end;


begin
  randomize;
  lchar:='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  for i:=1 to t do
  Result := Result + lchar[Random(62)+1];
  if t > 8 then
    Exit;
  case t of
    7:
      begin
        Result := Result + '1234567';
        Exit;
      end;


    8:
      begin
        Result := Result +'12345678';
        Exit;
      end
  end;
  SetLength(k, t);
  i := 0;
  while true do
  begin
    if i >= t then
      Break;
    j := Random(8);
    if (j = 0) or isin(j) then
    begin
      Continue;
    end;
    k[i] := j;
    Inc(i);
  end;
  for i := 0 to t - 1 do
    Result := Result + Format('%d', [k[i]]);
  SetLength(k, 0);

end;


\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

效率高的还是这个

function getint2(t: integer): string;
var
  i: Integer;
  lchar: string;
  lc: Char;
begin
   lchar:='12345678';
   randomize;
  while true  do
  begin
     if Length(Result) = t then Break;
     lc := lchar[Random(8)+1] ;
     if lc = '' then Continue;
     if Pos(lc,Result) > 0 then Continue;
     Result := Result + lc;
  end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑贝是条狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值