J’ai fini…
En fait, il faut modifier la valeur de la constante nmax…
Et il fallait aussi optimiser la taille du tableau de matchs…
Voila le code, commenté en prime, pour ceux que ça intéresse :
Program DH1Exercice2Q2;
Uses CRT;
Const
nmax=177;
Type
tableau=array[1..nmax] of integer;
tableau2=array[1..nmax,1..nmax] of integer;
tableau_matchs=array[1..nmax,1..2] of integer;
Var
A,adv:tableau;
joueurs:tableau2;
matchs:tableau_matchs;
winner,ind_dpge:integer;
{*** Procédure qui affiche le contenu d'un tableau ***}
Procedure ecriture(t:tableau;taille:integer);
var
i:integer;
begin
for i:=1 to taille do
write(t[i],' ');
writeln;
end;
{*** Procédure qui génère un tableau aléatoirement ***}
Procedure initialisation(var t:tableau;taille:integer);
var
i:integer;
begin
randomize;
for i:=1 to taille do
t[i]:=random(100);
ecriture(t,taille);
end;
{*** Procédure DPGE, qui calcule le plus grand et le 2e plus grand élément d'un tableau t ***}
Procedure DPGE(t:tableau;taille:integer;var winner,ind_dpge:integer);
Var
n,i,tour,k,num_match,num_adv:integer;
Begin
n:=taille;
{*** On remplit le tableau des joueurs au premier tour avec leur indice dans le tableau t ***}
for i:=1 to n do
joueurs[1,i]:=i;
{*** On passe au tour 2 ***}
tour:=2;
{*** On initialise le numéro du match à 1 ***}
num_match:=1;
while (n <> 1) do
{*** Tant qu'il ne reste pas qu'un seul joueur ***}
begin
k:=n div 2;
for i:=1 to k do
{*** On joue les matchs du tour suivant ***}
begin
if (t[joueurs[tour-1,2*i-1]] > t[joueurs[tour-1,2*i]]) then
joueurs[tour,i]:=joueurs[tour-1,2*i-1]
else
joueurs[tour,i]:=joueurs[tour-1,2*i];
{*** Dans joueurs[tour,i], on stocke l'indice du vainqueur du match ***}
matchs[num_match,1]:=joueurs[tour-1,2*i-1];
matchs[num_match,2]:=joueurs[tour-1,2*i];
num_match:=num_match+1;
{*** On stocke les 2 joueurs du matchs dans le tableau matchs ***}
end;
if (n mod 2 = 0) then
n:=k
else
begin
joueurs[tour,k+1]:=joueurs[tour-1,n];
n:=k+1;
end;
{*** Ce if sert à déterminer le nombre de joueurs du tour suivant ***}
tour:=tour+1;
end;
tour:=tour-1;
num_match:=num_match-1;
winner:=joueurs[tour,1];
{*** Le plus grand élément de tout le tableau est dans winner ***}
num_adv:=1;
for i:=1 to num_match do
{*** On parcourt le tableau matchs pour trouver les adversaires de winner ***}
begin
if (matchs[i,1] = winner) then
begin
adv[num_adv]:=matchs[i,2];
num_adv:=num_adv+1;
end
else
begin
if (matchs[i,2] = winner) then
begin
adv[num_adv]:=matchs[i,1];
num_adv:=num_adv+1;
end;
end;
end;
{*** Les adversaires de winner sont dans adv ***}
num_adv:=num_adv-1;
ind_dpge:=adv[1];
for i:=2 to num_adv do
if (t[adv[i]] > t[ind_dpge]) then
ind_dpge:=adv[i];
{*** On cherche le plus grand élément de adv, qui est le 2e plus grand élément cherché ***}
End;
Begin
clrscr;
initialisation(A,8);
DPGE(A,8,winner,ind_dpge);
Writeln('Plus grand élément : ',A[winner],' (indice : ',winner,')');
Writeln('Deuxième plus grand élément : ',A[ind_dpge],' (indice : ',ind_dpge,')');
readkey;
End.