Etapes de la correction

 

1- Exponentition rapide

2- Définir un type Matrice

3- Procédure de fusion

4- Algorithme

Retour à la page principale

 

 

        1)FUNCTION PuissRec (x: REAL; n: WORD): REAL;

BEGIN

IF n=0

THEN PuissRec:=l

ELSE

IF Odd(n)

THEN PuissRec:=x*PuissRec(x*x,n DIV 2)

ELSE PuissRec:=PuissRec(x*x,n DIV 2)

End;

FUNCTION PuissIter(x:REAL;n:WORD):REAL;

VAR res:REAL;

BEGIN

Res:=l;

WHILE n< >0 DO BEGIN

IF Odd(n) THEN res:=res*x;

X:=X*X;

n:=n DIV 2;

END;

PuissIter:=res;

end;

retour au menu

                    2)TYPE Matrice=ARRAY[1. .2,1..2] OF WORD;

CONST trans:Matrice=((0, l) , (1, 0));

PROCEDURE Prod(a,b: Matrice; VAR res:Matrice);

BEGIN

res [1,1] :=a[l, 1]*b[1,l] +a[l,2]*b[2,1] ;res[l,2]:=a[1,1]*b[l,2] +a[l,2]*b[2,2];

res [2,1] :=a[2,1]*b[1,l] +a[2,2]*b[2,1] ;res[2,2]:=a[2,1]*b[l,2] +a[2,2]*b[2,2];

end;

PROCEDURE Puiss (a: Matrice; n: WORD; VAR res:Matrice);

VAR a2:Matrice;

BEGIN

IF n=0

THEN BEGIN

res[1,1]:=l;res[1,2]:=0;res[2,1]:=0;res[2,2]:=l;

END

ELSE BEGIN

IF n MOD 2 = 0

THEN BEGIN

Prod(a,a,a);

Puiss(a,n DIV 2,res);

END

ELSE BEGIN

Prod(a,a,a2);

Puiss (a2, n DIV 2, res)

Prod(a,res,res);

END

END;

END;

FUNCTION Fib(n:WORD) :WORD;

VAR mat:Matrice;

BEGIN

Puiss (trans,n, mat);

Fib:=mat [1, 2];

End;

retour au menu

3) PROCEDURE TriFusion(VAR t:Tableau; n: Indice);

VAR temp:Tableau;

PROCEDURE Fusion(g,m,d:Indice);

VAR h,i,j,k:Indice;

BEGIN

i:=g;j:=m+l;k:=g;

WHILE (i<=m) AND (j<=d) DO BEGIN

IF t[i]<=t[j]

THEN BEGIN

temp[]:=t[i];i:=i+1;

END

ELSE BEGIN

temp[k]:=t[j];j:=j+l;

END;

k:=k+1;

END;

FOR h:=i TO m DO BEGIN

temp[k]:=t[h];

k:=k+1;

END;

FOR h:=j TO d DO BEGIN

temp[k]:=t[h];

k:=k+l;

END;

FOR h:=g TO d DO t[h]:=temp[h];

END;

PROCEDURE Tri(g,d:Indice);

VAR m:Indice;

BEGIN

IF g<d THEN BEGIN

m:=(g+d) DIV 2;

Tri(g,m);

Tri(m+l,d);

Fusion(g,m,d);

END;

END;

BEGIN

Tri(l,n);

END;

retour au menu

        4) FUNCTION k_ieme(VAR t:Tableau; n,k:.Indice):Element;

VAR pivot: Element;

FUNCTION Partition (g, d:Indice): Indice;

VAR I,J:Indice;

BEGIN

i:=g;

FOR j:=g+l TO d DO

IF t [j] <pivot

THEN BEGIN

i:=i+l;

Echange (t [i],t [j])

END;

Echange(t [g], t [i]);

Partition:=i;

END;

FUNCTION k_aux(g,d:Indice):Element;

VAR p:Indice;

BEGIN

pivot:=t[g];

p:=Partition(g,d);

IF p>k

THEN k_aux:=k_aux(g,p-1)

ELSE

IF p=k

THEN k_aux:=pivot

ELSE k_aux:=k_aux(p+l,d);

END;

BEGIN k_ieme:=k_aux(1,n);

END;

retour au menu

Retour à la page principale