2013-04-06 57 views
-1

我在这里有一个很大的问题,甚至不知道如何下手......德尔福 - 随意组合(数学)

在简短的说明,我需要知道,如果一个号码是一组从结果随机组合...

让我更好地解释:我创建了一个随机“数”具有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; 

现在我需要知道的是一些其他的随机数,我们假设“324”(例子)在该随机组合的结果集合中。

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


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

请首先检查这个环节https://en.wikipedia.org/wiki/Combination

一旦我得到一些用户键入的数字,在一个编辑框中,我需要检查它是否在这个随机组合中: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; 
+4

我不明白问题 – 2013-04-06 11:39:24

+1

OP也没有。 :-) – 2013-04-06 14:46:17

+0

我同意你们......对不起,我会在问题中添加更多信息。这是因为我的英语不太好,这个数学问题很棘手! – Guybrush 2013-04-06 19:54:52

回答

-1
begin 
    Randomize; //only need to execute this once. 
    sTmp := ''; 
    While (Length(sTmp) < 3) Do 
    Begin 
    cTmp := IntToStr(Random(7) + 1)[1]; // RANDOM(7) produces # from 0..6 
             // so result will be '1'..'7', not '8' 
             // Alternative: clmp := chr(48 + random(8)); 
    If (Pos(cTmp, sTmp) = 0) Then 
     sTmp := sTmp + cTmp; 
    IF SLMP = '324' THEN 
     DOSOMETHING; // don't know what you actually want to do 
        // Perhaps SET SLMP=''; to make sure '324' 
        // isn't generated? 
    end; 
    edt1.Text := sTmp; 
end; 
+0

代码只有这样的答案很薄弱。你需要写一些文字。 – 2013-04-06 11:54:02

+0

问题含糊不清,答案并不模糊,只是对所有人都是不正确的,只有一种可能的情况,因此毫无价值。 – 2013-04-06 14:47:14

+0

谢谢!请检查我添加的新信息。 – Guybrush 2013-04-06 21:32:57

1

你有你的发电机。一旦你的价值是建立,这样做

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,但它会是假的!)

修订: 我不喜欢嵌套循环^^。这是一个返回整数的第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开始的数字,从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; 
+0

谢谢格雷格M.,试图帮助,但它不是我所需要的。请检查我添加的新信息。这是一种诡计来解释...... – Guybrush 2013-04-06 21:32:08

+0

好吧,你不使用字符串。你只需要检查你的int值是否在1到8之间,并且你有三个数字。 123是有效的,而12,923则不是。而已 ? – 2013-04-07 07:26:48

0

问题似乎并不过于空泛给我, 也许有点不舒服说明。

从我的理解你想检查一个字符串是否在一组随机生成的字符。

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

从目标字符串中减去每个字母 如果排序后的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 

德尔福石头!

+0

嗨Kapytanhook,谢谢!我会尝试你的功能... – Guybrush 2013-04-06 20:12:51

+0

那么,它没有工作,但这是我的错,因为我没有很好地解释这个问题......对不起。 – Guybrush 2013-04-06 21:29:43

+0

请检查我添加的新信息。谢谢! – Guybrush 2013-04-06 21:36:10

1

你想测试是否有某种组合。要做到这一点,你需要确认假定的组合满足以下条件:

  1. 每个元素的范围是1..N和
  2. 没有元素多次出现。

所以,像这样实施它。

  • 声明一个计数数组,例如Integer的数组[1..N]。如果N在运行时变化,您将需要一个动态数组。
  • 将数组的所有成员初始化为零。
  • 循环通过推定组合的每个元素。检查元素是否在1..N范围内。并增加该元素的计数。
  • 如果任何元素的计数大于1,那么这不是有效的组合。

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