fork download
  1. program Project1;
  2.  
  3. type Gender = (Female, Male);
  4.  
  5. type Child = record
  6. g : Gender;
  7. dw : integer;
  8. end;
  9.  
  10. type Farmer = record
  11. c : array [0..1] of Child;
  12. end;
  13.  
  14. procedure GenerateChild (var c : Child);
  15. begin
  16. if random(2)=0 then c.g := Female else c.g := Male;
  17. c.dw := random(7);
  18. end;
  19.  
  20. type FarmerAnswer = record
  21. goc : Gender;
  22. dwoc : integer;
  23. end;
  24.  
  25. procedure GenerateFarmer(var f: Farmer);
  26. begin
  27. GenerateChild (f.c[0]);
  28. GenerateChild (f.c[1]);
  29. end;
  30.  
  31. procedure GetAnswer (const F : Farmer; var FA : FarmerAnswer);
  32. var
  33. ci : integer;
  34. begin
  35. if (F.c[0].g = Male) and (F.c[1].g = Female) then
  36. ci := 0
  37. else if (F.c[0].g = Female) and (F.c[1].g = Male) then
  38. ci := 1 // ÷òîáû 1/3 ïîëó÷èòü - âûáèðàåì èìåííî ñûíà
  39. else
  40. ci := random(2);
  41.  
  42. FA.goc := F.c[ci].g;
  43. FA.dwoc := F.c[ci].dw;
  44.  
  45. end;
  46.  
  47. var
  48. i : integer;
  49. F : Farmer;
  50. FA : FarmerAnswer;
  51. Count, Success : integer;
  52. begin
  53. Randomize;
  54. Count := 0;
  55. Success := 0;
  56. for i := 0 to 1000*1000-1 do begin
  57. GenerateFarmer(F);
  58. GetAnswer(F, FA);
  59. if (FA.goc=Male) and (FA.dwoc=1) then begin
  60. Inc(Count);
  61. if (F.c[0].g = Male) and (F.c[1].g = Male) then
  62. Inc(Success);
  63. end;
  64. end;
  65.  
  66. WriteLn('Count=', Count, '; Success=', Success, '; Ratio=', Success/Count:0:10);
  67. end.
Success #stdin #stdout 0.5s 2052KB
stdin
Standard input is empty
stdout
Count=107314; Success=35633; Ratio=0.3320442813