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
| #include <stdio.h>
#include <stdlib.h>
#define INFINITY 100
double fact(int n);
int fact2(int n);
int main(void){
//Estimation de la valeur de e: Somme_{n=0}^INFINI 1/n! La valeur de e est 2.718282
double e = 0;
int n = 0;
while(n<INFINITY){
/*
double fact_n = 1;
int i;
for(i=1;i<=n;i++){
fact_n *= i;
}
*/
e+= 1/fact(n);
n++;
}
printf("La valeur de e est %lf\n\n----------------\n\n",e);
//Nombre de podiums de 3 athlètes possibles parmi n:
// n!/(n-3)!
int athletes;
printf("Combien y a-t-il d'athlètes en compétition ?\n");
scanf("%d",&athletes);
/*
int fact_athletes = 1;
int fact_athletes_moins = 1;
*/
if(athletes>=3){
/*
int i;
for(i=1;i<=athletes;i++){
fact_athletes *= i;
}
for(i=1;i<=athletes-3;i++){
fact_athletes_moins *= i;
}
printf("Nombre de podiums possibles:%d\n",fact_athletes/fact_athletes_moins);
*/
printf("Nombre de podiums possibles:%d\n",fact2(athletes)/fact2(athletes-3));
}
else{
printf("Pas assez d'athlètes pour remplir le podium !\n");
}
return EXIT_SUCCESS;
}
/**************************************Les fonctions**************************************/
//factorielle
//PRE :/
//POST : renvoi la factorielle (reel) du nombre n
double fact(int n){
double r;
//invariant : n est plus grand que 0
for(r=1;n>0;n--){
r*=n;
}
return r;
}
//PRE :/
//POST : renvoi la factorielle (entier) du nombre n
int fact2(int n){
int r;
//invariant : n est plus grand que 0
for(r=1;n>0;n--){
r*=n;
}
return r;
} |