Delphi-随机组合(数学) [英] Delphi - Random Combination (Math)

查看:114
本文介绍了Delphi-随机组合(数学)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到大问题,甚至都不知道该如何开始...

I have a BIG problem here and do not even know how to start...

简而言之,我需要知道随机组合的结果集中是否包含数字...

In short explanation, I need to know if a number is in a set of results from a random combination...

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

Let me explain better: I created a random "number" with 3 integer chars from 1 to 8, like this:

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;

现在我需要知道其他随机数,比如说"324"(示例),位于该随机组合的结果集中.

Now I need to know is some other random number, let's say "324" (example), is in the set of results of that random combination.

请,有人可以帮忙吗?一个获得方程式来解决这个问题的链接就足够了...

Please, someone can help? A link to get the equations to solve this problem will be enough...

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

Ok, let me try to add some useful information:

请首先检查此链接 https://en.wikipedia.org/wiki/Combination

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

Once I get some number typed by user, in an editbox, I need to check if it is in the set of this random combination: S = (1..8) and k = 3

整rick,哼?

这就是我得到的.也许对将来的某人有用.感谢所有尝试提供帮助的人!

Here is what I got. Maybe it be usefull for someone in the future. Thank you for all people that tried to help!

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;

推荐答案

您拥有生成器.建立价值之后,请执行

You have your generator. Once your value is built, do something like

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!)

Note : it works only because you have unique digits, the digit 3 is only once in you final number. For something more generic, you'll have to tweak this function. (testing "3","3","2" would return true but it would be false !)

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

UPDATED : I dislike the nested loop ^^. Here is a function that return the nTh digit of an integer. It will return -1 if the digits do not exists. :

 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开始从右到左的数字.它将返回数字的值.

nDigits is the digits number from right to left starting at 1. It will return the value of the digit.

GetDigits( 234, 1) returns 4
GetDigits( 234, 2) returns 3
GetDigits( 234, 3) returns 2.
GetDigits( 234, 4) returns 0.

现在,此最后一个函数检查值是否是一个很好的组合,并指定您要查找的最大位数:

Now this last function checks if a value is a good combination, specifying the maxdigits you're looking for :

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;

这篇关于Delphi-随机组合(数学)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆