Exercices corrigés sur les fonctions récursives-TD2-
Expliquez les fonctionnalités des fonctions suivantes :
Exercice 1
FONCTION fct1(tab[],n)
x=-1
SI n=1 ALORS
retourne tab[0]
SINON
x=fct1(tab,n-1)
FINSI
SI x> tab[n-1] ALORS
retourne x
SINON
retourne tab[n-1]
SINSI
FINFONCTION
def fct1(tab, n):
if n == 1:
return tab[0]
else:
x = fcttt(tab, n-1)
if x > tab[n-1]:
return x
else:
return tab[n-1]
Corrigé :
fct1() renvoie la valeur maximale dans le tableau d'entrée tab[] de taille n.
Exercice 2
FONCTION fct2(a,b)
SI b=0 ALORS
retourne 1
FINSI
SI (b mod 2) =0 ALORS
retourne fct2(a*a,b/2)
FINSI
retourne fct2(a*a,b/2)*a
FINFONCTION
def fct2(a, b):
if b==0:
return 1
if b%2 ==0:
return fct2(a*a,b//2)
return fct2(a*a,b//2)*a
Corrigé :
fct2() renvoie ab
Partager ce cours avec tes amis :
Rédigé par
ESSADDOUKI
Mostafa
The education of the 21st century opens up opportunities to not merely teach, but to coach, mentor, nurture and inspire.
