2013-04-06 52 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,那麼這不是有效的組合。

現在你可以通過用布爾數組替換整數數組來簡化它,但這應該是不言而喻的。