« Algorithmique/Exercices et solutions » : différence entre les versions

Contenu supprimé Contenu ajouté
fisrt draft
 
→‎Exercices et quelques solutions : copie de https://fr.wikipedia.org/wiki/Utilisateur:Npettiaux#R.C3.A9solution_collective_d.27exercices
Ligne 2 :
 
Dans un premier temps, par facilité, la numérotation des solutions correspond à celle de la [https://github.com/HEB-ESI/DEV2-ALG-Syllabus-Algo/blob/master/dev2-alg-syllabus.pdf version pdf imprimable] de septembre 2015.
 
= Résolution collective d'exercices =
 
'''ATTENTION''' tous les algorithmes proposés ci-dessous n'ont '''pas nécessairement été relus ni corrigés''', ni par un enseignant ni même parfois par des étudiants ayant un oeil assez critique.
 
Le lecteur DOIT donc considérer qu'il s'agit d'une '''première version''' et est invité à '''apporter des explications et des améliorations''' lorsque ceci est pertinent.
 
Il convient en particulier souvent de compléter l'algorithme par une précision des données, des inconnues ou de l'objectif puis de la méthode de résolution avec un schéma ou une (des) phrase(s) d'explication.
 
Ceci est un recueil de propositions de solutions par des étudiants, pour des étudiants. Ceci n'est '''intéressant QUE et SI''' les étudiants font avant la lecture de la solution, '''un essai personnel''', peut-être en groupe, de résolution active.
 
== Classe ESI201516A32_ALG ==
 
=== Ex 4.14 ===
 
heure (entier) \
minutes (entier) - totalSecondes (entier) = heure * 3600 + minutes * 60 + secondes
secondes (entier) /
 
module conversionHMS2secondes()
// hms2sec() // autre nom plus court
heures : entier
minutes : entier
secondes : entier
totalSecondes : entier
totalSecondes <= heures * 3600 + minutes * 60 + secondes
fin_module
 
=== Ex 4.11 : allureEnKmparHeure() ===
 
heure (entier) \
min (entier) - allure = heure + min / 60 + sec / 3600 (réel)
sec (entier) /
vitesse = 1/ allure (réel)
'''module''' allureEnkmparHeure()
heure : entier
min : entier
sec : entier
allure, vitesse : réel
allure <= heure + min / 60 + sec / 3600
vitesse <= 1/ allure
'''fin_module'''
 
=== Ex 5.10 : prixTTC() ===
D:- Un prix HTVA
- La TVA : 21% du prix HTVA
I: Prix TTC
'''module''' prixTTC()
prixHTVA, TVA, prixTTC : réel
TVA <- 21
prixTTC <- prixHTVA+(prixHTVA/100*TVA)
'''fin_module'''
 
== Ex 5.2 ==
 
D: nombre1 (réel)
nombre2 (réel)
I: moyenne (réel)
R: moyenne <--- (nombre1 + nombre2) / 2
'''algorithme moyenne'''(nombre1+nombre2 : réels) --->réel
'''retourner''' (nombre1 +nombre2) / 2
'''fin algorithme'''
 
==Ex 5.3==
 
Données :
* baseTriangle(réel)
* hauteurTriangle (réel)
Inconnues ou objectif : surfaceTriangle (réel)
Résolution :
surfaceTriangle <---(baseTriangle * hauteurTriangle) /2
'''algorithme''' surfaceTriangle(baseTriangle, hauteurTriangle :réels) --->réel
'''retourner''' (baseTriangle * hauteurTriangle) / 2
'''fin algorithme'''
 
==Ex 6.5==
 
'''algorithme max'''(nb1, nb2 : réels) -->réel
max: réel
'''si''' nb1>nb2'''alors'''
max ←nb1
'''sinon'''
max ←nb2
'''fin si'''
'''retourner''' max
'''fin algorithme'''
 
== Ex 6.6 ==
'''algorithme salaireNet''' (salaireBrut, retenue speciale : réels) -->réel
salaireNet:réel
'''si''' salaireBrut > 1200 '''alors'''
salaireNet ←(salaireBrut – (salaireBrut-1200)*15%)
'''fin si'''
'''fin algorithme'''
 
== Ex 6.7==
 
à discuter et corriger car pas tout à fait correct
 
'''algorithme fonctionSyracuse''' (n:entier) -->réel
f(x) : réel
'''si''' n MOD 2 = 0 '''alors'''
f(x) ← n/2
'''sinon'''
f(x) ← (3n +1)
'''fin si'''
'''retourner f(x)'''
'''fin algorithme'''
j'ai une autre version, maintenant faut savoir si elle est juste ou pas.(hamza, de A322)
a'''lgorithme fonctionSyracus (n:entier)-->réel'''
 
résultat:réel
 
'''si nb1 = pair alors'''
 
'''nb1<--n/2'''
 
'''sinon nb1<--3n+1'''
 
'''fin si'''
 
'''retourner résultat'''
 
'''fin algorithme'''
 
== Ex 6.8==
 
algorithme tarifReduit (droit :booléen)->réel
'''ok''' ←7
'''si ok'''
'''sinon'''
'''ok''' ←8
'''fin si'''
'''retourner ok'''
'''fin algorithme'''
'''autre version:''' par (hamza de A322)
 
'''algorithme tarifReduit (tarif:entier)-->boolean'''
 
ok'''-->''' boolean
 
ok<--a une tarif réduit
 
'''si''' ok '''<--''' age'''<'''18ans '''alors'''
 
ok'''='''vrai
 
'''sinon'''
 
ok '''<--''' age'''>'''18ans '''alors'''
 
ok=faux
 
'''fin si'''
 
'''retourner ok'''
 
'''fin algorithme'''
 
== Ex 6.9==
'''algorithme maximumDe3Nombre (nb1,nb2,nb3->entier)->entier'''
 
'''maximum<--entier'''
 
'''si''' nb1>nb2 alors
 
max<--nb1
 
'''sinon si'''
 
nb2<nb3 alors
 
max<--nb3
 
'''sinon'''
 
max<--nb2
 
'''fin si'''
 
'''retourner le maximum'''
 
'''fin algorithme'''
 
== Ex 6.10 ==
 
'''algorithme signeNombre( nb:entier)'''
'''si''' (nb = 0) '''alors'''
'''afficher''' ''“ Le nombre est nul”''
'''sinon si''' (nb >0) '''alors'''
'''afficher''' ''“ Le nombre est positif”''
'''sinon'''
'''afficher''' ''“ Le nombre est negatif”''
'''fin si'''
'''fin algorithme'''
 
autre solution
 
'''algorithme signeNombre( nb:entier)''' => entier
// retourne +1 si positif, -1 si négatif, 0 si nul
resultat : entier
'''si''' (nb = 0) '''alors'''
résultat ← 0
'''sinon si''' (nb >0) '''alors'''
résultat ← +1
'''sinon'''
résultat ← -1
'''fin si'''
'''retourner''' résultat
'''fin algorithme'''
 
autre solution
 
'''algorithme signeNombre( nb:entier)'''
// affiche positif, négatif ou nul
resultat : chaine
'''si''' (nb = 0) '''alors'''
résultat ← "nul"
'''sinon si''' (nb >0) '''alors'''
résultat ← "positif"
'''sinon'''
résultat ← "négatif"
'''fin si'''
'''afficher''' résultat
'''fin algorithme'''
 
== Ex 6.11 ==
 
=== Énoncé ===
 
=== Solution ===
 
==== Schéma ====
 
==== Code ====
'''algorithme typeTriangle('''longeur1, longeur2, longeur3 :réels) → chaine
résultat : chaine
'''si''' (longeur1 = longeur2) ET (longeur1 = longeur3) '''alors'''
résultat ←”équilateral”
'''sinon'''
'''si''' (longeur1 = longeur2) OU (longeur2 = longeur3) OU (longeur1 = longeur3) '''alors'''
résultat ← ”isocele”
'''sinon'''
résultat ← ”scalène"
'''fin si'''
'''fin_si'''
'''retourner''' résultat
'''fin_algorithme'''
 
== Ex 6.12 ==
 
=== Énoncé ===
 
'''Dés identiques :'''
'''Écrire un algorithme qui lance trois dés et indique si on a obtenu 3 dés de valeur identique,'''
'''2 ou aucun.'''
 
=== Solution ===
 
à discuter et corriger car pas tout à fait correct
'''algorithme tripleDés()''' → chaîne
hasard1, hasard2, hasard3 : entiers
hasard1 ←hasard(6)
hasard2 ←hasard(6)
hasard3 ←hasard(6)
si (hasard1 = hasard2)ET (hasard1 = hasard3) alors
retourner (" 3 dés identique")
sinon
si (hasard1 = hasard2) OU (hasard1 = hasard3) OU (hasard2 = hasard3) alors
retourner ("2 dés identique")
sinon
retourner ("aucun dés identique")
fin_si
fin_si
fin algorithme
 
== Ex 6.8 ==
 
'''algorithme tarifReduit''' (reduction: boolean) -->entier
prix: entier;
ok ←reduction;
'''si''' ok '''alors'''
prix ←7
'''sinon'''
prix ←8
'''fin_si'''
retourner prix
fin algorithme
 
== Ex 6.13 ==
 
'''algorithme gradeEtudiant''' (moyenne:réel) → chaine
'''si''' (moyenne < 50 %) '''alors'''
grade ← ”pas reussi”
'''sinon si''' ( moyenne < 60%) '''alors'''
grade ← ”réussi”
'''sinon si''' ( moyenne <70%) alors
grade ← ”satisfaction” '''alors'''
'''sinon si''' ( moyenne < 80%) '''alors'''
grade ← ”distinction”
'''sinon si''' ( moyenne < 90%) '''alors'''
grade ← ”une grande distinction”
'''sinon''' ( moyenne <= 100%)
grade ← ”la plus grande distinction”
'''fin si'''
'''retourner grade'''
'''fin algorithme'''
 
== Ex 6.14 ==
 
=== Énoncé ===
Écrire un algorithme qui retourne le numéro du jour de la semaine reçu en paramètre
Ex: 1 pour lundi, 2 pour mardi, ...
 
=== Solution ===
 
'''algorithme NrJour''' (jour:chaine) → entier
nrJour: entier
'''si''' jour = lundi '''alors'''
nrJour ← 1
'''sinon si''' jour = mardi '''alors'''
nrJour ← 2
'''sinon si''' jour = mercredi '''alors'''
nrJour ← 3
'''sinon si''' jour = jeudi '''alors'''
nrJour ←4
'''sinon si''' jour = vendredi '''alors'''
nrJour ←5
'''sinon si''' jour = samedi '''alors'''
nrJour ←6
'''sinon''' jour = dimanche '''alors'''
nrJour ←7
'''fin_si'''
'''retourner''' nrJour
'''fin algorithme'''
 
== Ex 6.14 ==
=== Énoncé ===
Écrire un algorithme qui retourne le numéro du jour de la semaine reçu en paramètre
Ex: 1 pour lundi, 2 pour mardi, ...
 
=== Solution ===
 
'''algorithme NrJour''' (jour:chaine) → entier
nrJour: entier
'''selon que''' jour '''vaut'''
"lundi" :
nrJour ← 1
"mardi" :
nrJour ← 2
"mercredi" :
nrJour ← 3
"jeudi" :
nrJour ← 4
"vendredi" :
nrJour ← 5
"samedi" :
nrJour ← 6
"dimanche":
nrJour ← 7
'''fin_selon_que'''
'''retourner''' nrJour
'''fin_algorithme'''
 
== Ex 6.15==
 
 
 
'''algorithme''' tirerUneCarte ( Piocher, Signe: entiers)
Piocher ← hasard(13)
Signe ← hasard(4)
'''selon que''' Piocher vaut
 
1: Piocher ← "As"
 
2: Piocher ← "Deux"
 
3: Piocher ← "Trois"
 
4: Piocher ← "Quatre"
 
5: Piocher ← "Cinq"
 
6: Piocher ← "Six"
 
7: Piocher ← "Sept"
 
8: Piocher ← "Huit"
 
9: Piocher ← "Neuf"
 
10: Piocher ← "Dix"
 
11: Piocher ← "Valet"
 
12: Piocher ← "Dame"
 
13: Piocher ← "Roi"
 
'''fin selon que'''
 
 
 
'''selon que''' Signe vaut
 
1: Signe ← "Cœur"
 
2: Signe ← "Pique"
 
3: Signe ← "Carreau"
 
4: Signe ← "Trèfle"
 
'''fin selon que'''
 
afficher (Piocher,"de",Signe)
 
 
 
'''fin
algorithme'''
 
== Ex 6.16 ==
 
 
 
'''algorithme nbJourMois''' (nrMois: entier,) → entier
 
 
 
nrMois(1,2,3,4,5,6,7,8,9,10,11,12) ←(Jan, Fev, Mar,Avr, Mai,
Juin,Juil, Aout, Sept, Oct, Nov, Dec)
 
nbJourMois ←28,30,31
 
 
 
'''selon que''' nrMois '''vaut''' 1,3,5,7,8,10,12 nbJourMois ←31
 
'''selon que''' nrMois '''vaut''' 4,6,9,11 nbJourMois ←30
 
'''selon que''' nrMois '''vaut 2''' nbJourMois ←28
 
 
 
'''fin selon que'''
 
'''retourner
nbJourMois'''
 
'''fin algorithme'''
 
== Ex 6.18 ==
 
 
 
'''algorithme reussirGen1''' (cote1, cote2, cote3 : réels) -->boolean
ok : cote1>=50% ET cote2 >=50% ET cote3 >=50%
moyenneAA = (cote1/20 + cote2/20 + cote3/20)/3
'''si ok alors'''
'''afficher “Reussi”'''
'''si ok alors'''
'''afficher moyenneAA'''
'''sinon'''
'''afficher "Raté"
fin si<br>
fin algorithme'''
 
==Ex 6.20==
 
'''algorithme prixPhotocopies''' (nbPhotocopies : réel) → réel
prixPhotocopies: réel // euro
'''si''' nbPhotocopies <= 10 '''alors'''
prixPhotocopies ←10*0.10
'''si''' nbPhotocopies <= '''30 alors'''
prixPhotocopies ←(10*0.10) + (30-10)*0.09
'''si''' nbPhotocopies > 30 '''alors'''
prixPhotocopies ←(10*0.10) +(10*0.10) + (30-10)*0.09 +(nbPhotocopies – 30)*0.08
'''fin si'''
'''retourner prixPhotocopies'''
'''fin algorithme'''
 
 
==Ex 6.21==
 
 
 
'''algorithme
stationementAlternatif''' (nrJour,
nrMaison: entier)
→ boolean
 
 
 
'''si nrJour >=1
ET nrJour <=15'''
 
 
 
'''si''' nrMaison
MOD 2 =0 '''alors'''
 
'''returner Faux'''
 
'''si''' nrMaison
MOD 2 = 1 '''alors'''
 
'''returner Vrai'''
 
 
 
'''sinon si nrJour
>15 ET <=(28,30,31)'''
 
 
 
'''si''' nrMaison
MOD 2 = 0 '''alors'''
 
'''retourner Vrai'''
 
'''si''' nrMaison
MOD 2 = 1 '''alors'''
 
'''retourner Faux'''
 
'''fin si'''
 
'''fin algorithme'''