Explorer les Livres électroniques
Catégories
Explorer les Livres audio
Catégories
Explorer les Magazines
Catégories
Explorer les Documents
Catégories
Exercice 1 : Ecrire un programme qui demande un entier n positif et teste si ce nombre est
un multiple de 5 (sans utiliser les opérateurs /, mod, rem, ...).
Correction :
Algorithme : « Programme Principal »
Correction :
Fonction Fibonacci(n :entier) :entier
Debut
si (n=0) ou (n=1) alors Fibonacci 1
sinon Fibonacci Fibonacci(n-1) + Fibonacci(n-2)
finsi
fin
Calculer le terme un de la suite de Fibonacci
Fibb(1)=1
Fibb(5)
Fibb(4) Fibb(3)
Fibb(2) Fibb(1)
Fibb(3) Fibb(2)
Fibb(1) Fibb(0)
est incorrect.
2
Exercice 3 : Ecrire une fonction récursive ACKERMANN qui calcule A(a,b) selon le
principe suivant :
A(0,b) = b+1
A(a,0) = A(a-1,1)
A(a,b) = A(a-1, A(a,b-1))
Correction
Fonction ACKERMAN(a,b :entier) :entier
debut
si (a=0) alors ACKERMANb+1
sinon si (b=0) alors ACKERMAN ACKERMAN(a-1,1)
sinon ACKERMAN ACKERMAN(a-1, ACKERMAN(a,b-1))
finsi
finsi
fin
Exercice 4 :
a- Ecrire une fonction récursive de recherche simple d'un élément x dans un tableau
d'entiers de taille n non trié.
Correction :
Type TAB : tableau de [1..200] d’entiers
Fonction Recherche (Tab : tableau d’entier, n, x :entier) :booleen
Début
Si n=0
3
alors Recherche faux
sinon
Si Tab[n]= x alors Recherchevrai
Sinon
Recherche Recherche(Tab, n-1,x)
Finsi
Finsi
Fin
Correction
Type TAB : tableau de [1..200] d’entiers
Fonction Rech_Dichot (T : TAB, Deb, Fin, x :entier) :booleen
Debut
mil(Fin+Deb) div 2
si Deb>Fin alors Rech_Dichotfaux
sinon
Si T[mil] = x alors Rech_Dichotvrai
Sinon
si (T[mil] < x)
alors Rech_Dichot Rech_Dichot (T,mil+1,Fin, x)
Sinon Rech_Dichot Rech_Dichot (T,Deb,mil-1, x)
Finsi
Finsi
Fin
Exercice 5: Écrire une fonction récursive appelée PGCD qui calcule le PGCD de 2 entiers a
et b par la méthode d'Euclide.
Exemple : PGCD(49,35) = PGCD(35,14) = PGCD(14,7) = 7
Correction :
Fonction PGCD(A,B :entier) :entier
Début
Si A=B alors PGCDA
sinon si A>B alors PGCD PGCD(A-B,B)
sinon PGCD PGCD(A,B-A)
finsi
finsi
Fin
Correction :
4
Début
si a=0 alors sommeb
sinon
si b=0 alors sommea
sinon
si a>b alors somme1+somme(a,b-1)
sinon somme1+somme(a-1,b)
finsi
/* ce test permet d’accélérer l’éxécution en diminuant le nombre d’appels récursifs*/
finsi
finsi
fin
Fonction produit(a,b :entier) :entier
Début
si a=1 alors produitb
sinon si b=1 alors produita
sinon produita+produit(a,b-1)
finsi
fin
Exercice 7 :
a- Ecrire une procédure récursive qui permet d’afficher un tableau de caractères.
Correction :
Type : TAB : tableau de [1..100] d’entiers
Procédure Afficher (T : TAB ; i, n : entier)
Début
Si i<n alors
Ecrire (T[i])
Afficher (T,i+1,n)
Fin si
Fin
1ier appel appel récursif : Afficher (T ,1, n) :
Procédure Afficher2 (T : TAB ; n : entier)
Début
Si n>1 alors
Afficher (T,n-1)
Ecrire (T[n])
Fin si
Ecrire (T[n])
Fin
1ier appel appel récursif : Afficher (T ,1, n) :
5
Correction :
Procédure AfficherInverse (T : TAB ; i, n : entier)
Début
Si i<n alors
Afficher (T,i+1,n)
Ecrire (T[i])
Fin si
Ecrire (T[n])
Fin
1ier appel appel récursif : AfficherInverse (T ,1, n)
Procédure AfficherInverse2 (T : TAB ; n : entier)
Début
Si n>1 alors
Ecrire (T[n])
Afficher (T,n-1)
Fin si
Ecrire (T[1])
Fin
1ier appel appel récursif : AfficherInverse (T , n)
Exercice 8 :
Ecrire une fonction récursive qui retourne le maximum dans un tableau de n entier.
Correction :
Type : TAB : tableau de [1..100] d’entiers
Fonction maximum_tab(T : TAB ; i, n : entier, var max_courant : entier) : entier
Début
Si i=n alors
Si T[n]>max_courant alors
max_courantT[i]
Finsi
maximum_tab max_courant
Sinon
Si T[i]>max_courant alors
max_courantT[i]
Finsi
maximum_tab maximum_tab(T,i+1,n, max_courant)
Fin si
Fin
1ier appel appel récursif : Mmaximum_tab(T ,2, n ,T[1]) :