-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapitre3.cpp
312 lines (252 loc) · 6.73 KB
/
Chapitre3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// Created by Valentin on 09.10.2020.
//
#include <iostream>
#include <string>
#include <limits>
#include <cmath>
#include <iomanip>
using namespace std;
int ex3_8() {
int noMois;
cout << "Entrez un no de mois (1-12) :";
cin >> noMois;
// Pour ne pas avoir à utiliser la lib string:
// Utiliser le ternaire directement dans le cout
string jours = (noMois == 4 || noMois == 6 || noMois == 9 || noMois == 11) ?
"30" : noMois == 2 ? "28 ou 29" : "31";
cout << "Ce mois comporte " << jours << " jours." << endl;
return EXIT_SUCCESS;
}
int ex3_9() {
int a, b, c;
cout << "Entrez 3 entiers séparés par un espace :";
cin >> a >> b >> c;
int min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
{
// Autre variante
int min = a < b ? a : b;
min = c < min ? c : min;
}
cout << "Le minimum des 3 entiers est: " << min << endl;
return EXIT_SUCCESS;
}
int ex3_11() {
int n;
cout << "Entrez un entier n >= 0 :";
cin >> n;
if (n % 3 == 0 && n % 5 == 0) {
cout << n << " est un multiple de 3 et 5" << endl;
} else if (n % 3 == 0) {
cout << n << " est un multiple de 3" << endl;
} else if (n % 5 == 0) {
cout << n << " est un multiple de 5" << endl;
} else {
cout << n << " n'est ni un multiple de 3 ni un multiple de 5" << endl;
}
return EXIT_SUCCESS;
}
int ex3_12() {
const unsigned NB_NOTES = 4;
double n1, n2, n3, n4;
cout << "Entrez " << NB_NOTES << " notes [1-6] :";
cin >> n1 >> n2 >> n3 >> n4;
double moyenne = (n1 + n2 + n3 + n4) / NB_NOTES;
cout << fixed << setprecision(1)
<< "Moyenne = " << moyenne << " - ";
if (moyenne > 5.5) {
cout << "Excellent";
} else if (moyenne > 5) {
cout << "Très bien";
} else if (moyenne > 4.5) {
cout << "Bien";
} else if (moyenne >= 4.0) {
cout << "Moyen";
} else {
cout << "Insuffisant";
}
cout << endl;
return EXIT_SUCCESS;
}
int ex3_15() {
enum class Mois {
JANVIER = 1, FEVRIER, MARS, AVRIL, MAI, JUIN, JUILLET, AOUT,
SEPTEMBRE, OCTOBRE, NOVEMBRE, DECEMBRE
};
unsigned noMois;
cout << "Entrez un no de mois (1-12) :";
cin >> noMois;
cout << "Ce mois comporte ";
switch ((Mois) noMois) {
case Mois::FEVRIER:
cout << "28 ou 29";
break;
case Mois::AVRIL:
case Mois::JUIN:
case Mois::SEPTEMBRE:
case Mois::NOVEMBRE:
cout << "30";
break;
default:
cout << "31";
break;
}
cout << " jours." << endl;
return EXIT_SUCCESS;
}
int ex3_19() {
double tauxInteretAnnuel; // en %
cout << "Entrez un taux d'interet annuel (en %) :";
cin >> tauxInteretAnnuel;
// Avec boucle
// double montant;
// cout << "Entrez un montant :";
// cin >> montant;
// const double MONTANT_CIBLE = montant * 2;
// int annee = 0;
// while (montant < MONTANT_CIBLE) {
// ++annee;
// double interet = montant * tauxInteretAnnuel / 100.0;
// montant += interet;
// }
// Sans boucle
// https://fr.wikipedia.org/wiki/Int%C3%A9r%C3%AAts_compos%C3%A9s
const unsigned NB_ANNEES =
(unsigned) ceil(log(2) / log10(1 + tauxInteretAnnuel / 100));
cout << "Le montant aura double apres " << NB_ANNEES
<< "an" << (NB_ANNEES > 1 ? "s" : "") << "." << endl;
return EXIT_SUCCESS;
}
int ex3_20() {
const unsigned NB_COLONNES = 3;
const unsigned N_DEPART = 20; // > 0
const unsigned N_FIN = 1; // > 0 et <= N_DEPART
const int W = (int) log10(N_DEPART) + 1; // Nb de chiffres dans N_DEPART
// 1) Version while
unsigned n = N_DEPART, noColonne = 1;
while (n >= N_FIN) {
cout << setw(W) << n;
if (noColonne == NB_COLONNES) {
cout << endl;
noColonne = 0;
} else {
if (n != N_FIN) {
cout << " ";
}
}
noColonne++;
n--;
}
cout << endl << endl;
// 2) Version for
for (unsigned i = N_DEPART, noCol = 1; i >= N_FIN; ++noCol, --i) {
cout << setw(W) << i;
if (noCol == NB_COLONNES) {
cout << endl;
noCol = 0;
} else {
if (n != N_FIN) {
cout << " ";
}
}
}
return EXIT_SUCCESS;
}
int ex3_26() {
int hauteur;
bool saisieOK;
const char MOTIF = '*';
do {
cout << "Hauteur du triangle (h >= 0) :";
if (!(saisieOK = cin >> hauteur && hauteur >= 0)) {
cin.clear();
cout << "Saisie incorrecte. Veuillez recommencer." << endl;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (!saisieOK);
for (int noLigne = 1; noLigne <= hauteur; ++noLigne) {
for (int i = 1; i <= hauteur - noLigne; ++i) {
cout << ' ';
}
for (int i = 1; i <= 2 * noLigne - 1; ++i) {
cout << MOTIF;
}
cout << endl;
}
return EXIT_SUCCESS;
}
int ex3_29() {
unsigned m, n;
bool saisieOK;
do {
cout << "Entrez deux nombres entiers (> 0) :";
cin >> m >> n;
saisieOK = m > 0 && n > 0;
if (!saisieOK) {
cin.clear();
cout << "Saisie incorrecte. Veuillez recommencer." << endl;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (!saisieOK);
unsigned min, max;
n < m ? (min = n, max = m) : (min = m, max = n);
unsigned long long ppmc = max;
while (ppmc % min != 0) {
ppmc += max;
}
// Autre variante:
// Pseudo code: ppmc(a, b) = a * b / pgcd(a, b)
unsigned pgcd;
while (min != 0) { // Méthode d'Euclide
pgcd = max;
max = min;
min = pgcd % min;
}
pgcd = max;
// ppmc = (unsigned long long) n * m / pgcd
cout << "PPMC entre " << m << " et " << n << " : " << ppmc << endl;
return EXIT_SUCCESS;
}
int ex3_32() {
const unsigned NB_TIRS = 100000;
unsigned nbTirsCercle = 0;
const unsigned RAYON_CERCLE = 1;
srand((unsigned) time(NULL));
for (unsigned tir = 0; tir < NB_TIRS; ++tir) {
double x = rand() / (RAND_MAX / 2.0) - 1;
double y = rand() / (RAND_MAX / 2.0) - 1;
if (x * x + y * y <= RAYON_CERCLE) {
nbTirsCercle++;
}
}
const double PI = RAYON_CERCLE * 2.0 * RAYON_CERCLE * 2 * nbTirsCercle / NB_TIRS;
cout << fixed << setprecision(2)
<< "Estimation de pi : " << PI << endl;
return EXIT_SUCCESS;
}
int ex3_33() {
const unsigned
NB_PORTES = 3,
NB_EXPERIENCES = 1'000'000;
unsigned porteVoiture, porteJoueur;
srand((unsigned) time(NULL));
// On part du principe que le joueur maintient toujours son choix initial
unsigned compteur = 0;
for (unsigned i = 1; i < NB_EXPERIENCES; ++i) {
porteVoiture = (unsigned) rand() % NB_PORTES + 1;
porteJoueur = (unsigned) rand() % NB_PORTES + 1;
compteur += porteVoiture == porteJoueur;
}
cout << fixed << setprecision(3)
<< "Probabilite de succes si maintien du choix initial: "
<< (double) compteur / NB_EXPERIENCES << endl
<< "Probabilite de succes si changement du choix initial :"
<< (double) (NB_EXPERIENCES - compteur) / NB_EXPERIENCES << endl;
return EXIT_SUCCESS;
}