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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
Depth = 50;
Total = Depth * Depth;
type
TMatrice = array[0..Depth-1] of array[0..Depth-1] of boolean;
procedure MatriceInitialize(var aMatrice: TMatrice; aValue: boolean);
var X,Y:integer;
begin
for X := 0 to Depth-1 do
for Y := 0 to Depth-1 do
aMatrice[Y,X] := aValue;
end;
procedure MatriceRandom(var aMatrice: TMatrice; aValue: boolean);
var X, Y: integer;
D : integer;
begin
// (10% (0.1) minimum et 50% (0.5) maximum ) -1
D := round(Total*0.1) + random(round(Total*0.4)) - 1;
// alors pourquoi 0.4 : 0.5-0.1 = 0.4
// -1 pour être < 50%
while D > 0 do
begin
X := random(Depth);
Y := random(Depth);
if aMatrice[Y,X] <> aValue then
begin
aMatrice[Y,X] := aValue;
dec(D);
end;
end;
end;
function MatriceCheck(aMatrice: TMatrice; var aTrue, aFalse: integer; aValue: boolean): boolean;
var X,Y: integer;
begin
aTrue := 0;
aFalse:= 0;
result := false;
for X := 0 to Depth - 1 do
for Y := 0 to Depth - 1 do
if aMatrice[Y,X] then
inc(aTrue)
else
inc(aFalse);
if aValue then
result := aTrue < aFalse
else
result := aFalse < aTrue;
end;
procedure MatriceShow(aMatrice: TMatrice);
var X,Y: integer;
const
BTC: array[boolean] of char = ('0','1');
begin
for X := 0 to Depth - 1 do
begin
for Y := 0 to Depth - 1 do
write(BTC[aMatrice[Y,X]]);
writeln('');
end;
end;
var
M : TMatrice;
cT, cF: integer;
cR: boolean;
begin
Randomize;
MatriceInitialize(M, false);
MatriceRandom(M, true);
MatriceShow(M);
cR := MatriceCheck(M, cT, cF, true);
if cR then
writeln(format('Matrice Ok :'#10' 0 = %d (%.2f%%)'#10' 1 = %d (%.2f%%)'#10' Total = %d'#10,
[cF, cF/Total*100, cT, cT/Total*100, Total]))
else
writeln(format('Matrice Fail :'#10' 0 = %d (%.2f%%)'#10' 1 = %d (%.2f%%)'#10' Total = %d'#10,
[cF, cF/Total*100, cT, cT/Total*100, Total]));
readLn;
end. |
Partager