challenge n人中m人が当選するくじ

n人の中から公平にm人を選ぶ、くじ引きプログラムを作ってください。

Posted feedbacks - Delphi

Integerの動的配列をtypeで定義しています。
Delphiは配列操作が苦手なので、できるだけ使わないようにしています。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
type
  TDynIntArray = array of Integer;

function lot(n, m: Integer): TDynIntArray;
  function NumInArray(num: Integer; arr :TDynIntArray): Boolean;
  var
    x: Integer;
  begin
    Result := False;
    for x in arr do
      if x = num then
        Result := True;
  end;
var
  people: TDynIntArray;
  i: Integer;
  num_lot: Integer;
begin
  SetLength(people, m);
  Randomize;
  for i := 0 to m - 1 do
  begin
    repeat
      num_lot := Random(n) + 1;
    until not NumInArray(num_lot, people);
    people[i] := num_lot;
  end;
  Result := people;
end;

Index

Feed

Other

Link

Pathtraq

loading...