Delphi 德尔菲-随机组合(数学)

Delphi 德尔菲-随机组合(数学),delphi,math,random,combinations,Delphi,Math,Random,Combinations,我这里有一个大问题,甚至不知道如何开始 简而言之,我需要知道一个数字是否在一组随机组合的结果中 让我更好地解释一下:我创建了一个随机的“数字”,其中有3个整数字符,从1到8,如下所示: procedure TForm1.btn1Click(Sender: TObject); var cTmp: Char; sTmp: String[3]; begin sTmp := ''; While (Length(sTmp) < 3) Do Begin Randomize;

我这里有一个大问题,甚至不知道如何开始

简而言之,我需要知道一个数字是否在一组随机组合的结果中

让我更好地解释一下:我创建了一个随机的“数字”,其中有3个整数字符,从1到8,如下所示:

procedure TForm1.btn1Click(Sender: TObject);
var
  cTmp: Char;
  sTmp: String[3];
begin
  sTmp := '';
  While (Length(sTmp) < 3) Do
  Begin
    Randomize;
    cTmp := IntToStr(Random(7) + 1)[1];
    If (Pos(cTmp, sTmp) = 0) Then
      sTmp := sTmp + cTmp;
  end;
  edt1.Text := sTmp;
end;
程序TForm1.btn1单击(发送方:TObject);
变量
cTmp:Char;
sTmp:String[3];
开始
sTmp:='';
而(长度(sTmp)<3)Do
开始
随机化;
cTmp:=IntToStr(随机(7)+1)[1];
如果(位置(cTmp,sTmp)=0),则
sTmp:=sTmp+cTmp;
结束;
edt1.Text:=sTmp;
结束;
现在我需要知道的是另一个随机数,比如说“324”(示例),在那个随机组合的结果集中

拜托,有人能帮忙吗?一个链接来获得方程来解决这个问题就足够了


好的,让我尝试添加一些有用的信息:

请先查看此链接

一旦我得到用户在编辑框中键入的数字,我需要检查它是否在这个随机组合的集合中:S=(1..8)和k=3

狡猾,哼


这是我得到的。也许这对将来的人有用。感谢所有试图帮助你的人

Function IsNumOnSet(const Min, Max, Num: Integer): Boolean;
var
  X, Y, Z: Integer;
Begin
  Result := False;
  For X := Min to Max Do
    For Y := Min to Max Do
      For Z := Min to Max Do
        If (X <> Y) and (X <> Z) and (Y <> Z) Then
          If (X * 100 + Y * 10 + Z = Num) Then
          Begin
            Result := True;
            Exit;
          end;
end;
函数isnumset(const-Min,Max,Num:Integer):布尔值;
变量
十、 Y,Z:整数;
开始
结果:=假;
对于X:=最小到最大Do
对于Y:=最小到最大Do
对于Z:=最小到最大Do
如果(xy)和(xz)和(yz),那么
如果(X*100+Y*10+Z=Num),则
开始
结果:=真;
出口
结束;
结束;
开始
随机化//只需要执行一次。
sTmp:='';
而(长度(sTmp)<3)Do
开始
cTmp:=IntToStr(随机(7)+1)[1];//随机(7)从0到6生成#
//所以结果将是“1”…“7”,而不是“8”
//备选方案:clmp:=chr(48+随机(8));
如果(位置(cTmp,sTmp)=0),则
sTmp:=sTmp+cTmp;
如果SLMP='324',则
剂量测定法;//不知道你到底想做什么
//可能设置SLMP='';确保“324”
//不是生成的吗?
结束;
edt1.Text:=sTmp;
结束;

你有你的发电机。一旦你的价值建立起来,做一些类似的事情

function isValidCode( Digits : Array of Char;  Value : String ) : Boolean;
var 
    nI : Integer;
begin
       for nI := 0 to High(Digits) do
       begin
             result := Pos(Digits[nI], Value ) > 0;
             if not result then break;
       end;
end;
像这样打电话

 isValidCode(["3","2","4"], RandomValue);
注意:只有当您有唯一的数字时,它才有效,数字3在您的最终数字中只有一次。对于更通用的功能,您必须调整此函数。(测试“3”、“3”、“2”将返回true,但将返回false!)

已更新: 我不喜欢嵌套循环^^。这是一个返回整数第n位的函数。如果数字不存在,则返回-1:

 function TForm1.getDigits(value : integer; ndigits : Integer ) : Integer;
 var
    base : Integer;
 begin
       base := Round(IntPower( 10, ndigits-1 ));
       result := Trunc( value /  BASE ) mod 10;
 end;
nDigits是从右到左从1开始的数字。它将返回数字的值

GetDigits( 234, 1) returns 4
GetDigits( 234, 2) returns 3
GetDigits( 234, 3) returns 2.
GetDigits( 234, 4) returns 0.
现在,最后一个函数检查一个值是否是一个良好的组合,并指定要查找的maxdigits:

function isValidCombination( value : integer; MinVal, MaxVal : Integer; MaxDigits : Integer ) :  Boolean;
var
   Buff : Array[0..9] of Integer;
   nI, digit: Integer;
 begin
   ZeroMemory( @Buff, 10*4);

   // Store the count of digits for
   for nI := 1 to MaxDigits do
   begin
      digit := getDigits(value, nI);
      Buff[digit] :=  Buff[digit] + 1;
   end;

   // Check if the value is more than the number of digits.
   if Value >= Round(IntPower( 10, MaxDigits )) then
   begin
     result := False;
     exit;
   end;

   // Check if the value has less than MaxDigits. 
   if Value < Round(IntPower( 10, MaxDigits-1 )) then
   begin
      result := False;
      exit;
   end;


  result := true;
  for nI := 0 to 9 do
  begin
     // Exit if more than One occurence of digit.
     result := Buff[nI] < 2 ;
     if not result then break;

     // Check if digit is present and valid.
     result := (Buff[nI] = 0) or InRange( nI, MinVal, MaxVal );
     if not result then break;
  end;

end;
函数IsValidComposition(值:整数;最小值,最大值:整数;最大数字:整数):布尔;
变量
Buff:整数的数组[0..9];
nI,数字:整数;
开始
零内存(@Buff,10*4);
//存储的数字计数
对于nI:=1到maxDo
开始
数字:=getDigits(值,nI);
Buff[数字]:=Buff[数字]+1;
结束;
//检查该值是否大于位数。
如果值>=四舍五入(整数幂(10,最大位数)),则
开始
结果:=假;
出口
结束;
//检查该值是否小于MaxDigits。
如果值<四舍五入(IntPower(10,MaxDigits-1)),则
开始
结果:=假;
出口
结束;
结果:=真;
对于nI:=0到9 do
开始
//如果出现多个数字,则退出。
结果:=Buff[nI]<2;
如果没有结果,则中断;
//检查数字是否存在且有效。
结果:=(Buff[nI]=0)或范围(nI,MinVal,MaxVal);
如果没有结果,则中断;
结束;
结束;

这个问题对我来说并不太模糊, 也许说得有点不好

据我所知,您想检查字符串是否在一组随机生成的字符中

下面是如何以最快的速度工作,保持所有字母的排序数组,以及每个字母有多少次

从目标字符串中减去每个字母 如果排序的int数组中的任何值小于0,则表示无法从这些字符生成字符串

我使它只适用于不区分大小写的字符串,但通过使字母数组长度为255个字符,而不是从A开始,它可以轻松地适用于任何字符串

这将不允许像另一个示例那样使用两次字符 所以“boom”不在“b”o“m”中

希望这对你有帮助

function TForm1.isWordInArray(word: string; arr: array of Char):Boolean;
 var
   alphabetCount: array[0..25] of Integer;
   i, baseval, position : Integer;
   s: String;
   c: Char;
begin
  for i := 0  to 25 do alphabetCount[i] := 0;  // init alphabet
  s := UpperCase(word); // make string uppercase
  baseval := Ord('A'); // count A as the 0th letter
  for i := 0 to Length(arr)-1 do begin // disect array and build alhabet
    c := UpCase(arr[i]); // get current letter
    inc(alphabetCount[(Ord(c)-baseval)]); // add 1 to the letter count for that letter
  end;
  for i := 1 to Length(s) do begin // disect string
    c := s[i]; // get current letter
    position := (Ord(c)-baseval);
    if(alphabetCount[position]>0) then // if there is still latters of that kind left
      dec(alphabetCount[position]) // delete 1 to the letter count for that letter
    else begin // letternot there!, exit with a negative result
      Result := False;
      Exit;
    end;
  end;
  Result := True; // all tests where passed, the string is in the array
end;
这样实施:

if isWordInArray('Delphi',['d','l','e','P','i','h']) then Caption := 'Yup' else Caption := 'Nope';  //yup
if isWordInArray('boom',['b','o','m']) then Caption := 'Yup' else Caption := 'Nope';  //nope, a char can only be used once

德尔菲岩石

您想测试某个东西是否是一个组合。为此,您需要验证假定的组合是否满足以下条件:

  • 每个元素的范围为1..N和
  • 没有元素出现多次 所以,像这样实现它

    • 声明计数数组,比如整数数组[1..N]。如果N在运行时发生变化,则需要一个动态数组
    • 将数组的所有成员初始化为零
    • 循环遍历假定组合的每个元素。检查元素是否在1..N范围内。并增加该元素的计数
    • 如果任何元素的计数大于1,则这不是有效的组合

    现在,您可以通过用布尔数组替换整数数组来简化,但这应该是不言而喻的

    我不明白这个问题,OP也不明白:-)我同意你们的观点。。。对不起,我将在问题中添加更多信息。这是因为我的英语不太好,而这道数学题很棘手!你的解决方案很差。你为什么不按我的建议做呢?您的版本在N=8时循环512次,执行lot
    if isWordInArray('Delphi',['d','l','e','P','i','h']) then Caption := 'Yup' else Caption := 'Nope';  //yup
    if isWordInArray('boom',['b','o','m']) then Caption := 'Yup' else Caption := 'Nope';  //nope, a char can only be used once