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
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define GAP(a,b) (((a)>(b))?((a)-(b)):((b)-(a)))
//prototypes
int initialisation (int *nbtires);
int calculuser(int cible, int *tab, int taille);
int operation(int n1, int n2, int operateur);
int best_tot = 0, best_gap = 999;
char str_result[255],str_tmp[255];
int main(void)
{
int nbc, taille=6; //nb cible
int nbtir[6];
//appel des fonctions
nbc=initialisation(nbtir);
if (!calculuser(nbc,nbtir,taille) ) calculuser(best_tot,nbtir,taille);
printf("\n");
//affichage solution
return printf(str_result);
}
//Initialisation du jeu : 6 nbs aléatoires tirés + 1 nb à trouver
int initialisation (int *nbtires)
{
int indice,i,j;
int nbcible;
int nbpossible[14]={1,2,3,4,5,6,7,8,9,10,25,50,75,100}; //valeurs départ possibles
srand(time(0));
printf("Tirage : ");
for (i=0;i<6;i++)
{
indice= rand()%14;
// if (nbpossible[indice] < 10) //pour faire apparaitre 2 fois un nombre <10
// {
// nbtires[i] = nbpossible[indice]; //nombres tirés au hasard
// printf("%i \t",nbpossible[indice]);
// fflush(NULL);
// }
for(j=0;j<6;j++)
{
if (nbtires[j] == nbpossible[indice]) //si 2 fois mm nb, retour au début du for(i)
continue;
if (j == 5) //si boucle finie, on stocke le nb tiré
{
nbtires[i] = nbpossible[indice]; //nombres tirés au hasard
printf("%i \t",nbpossible[indice]);
}
}
}
nbcible=(rand()%900)+100;//nombre à trouver enter 100 et 999
printf("\n\nVous devez trouver : %i\n\n",nbcible);
return (nbcible);
}
//calcul de la solution du nombre à trouver
int calculuser(int cible, int *tab, int taille)
{
int i,j,o; //boucles sur nb1, nb2, operation
int t[6];
char operateurs[4]={'+','-','*','/'}; //les 4 opérations
for (i=0;i<taille-1;i++)
{
for (j=i+1;j<taille;j++)
{
for(o=0;o<4;o++)
{
memcpy(t,tab,sizeof(int)*6);
//t contient les résultat des opérations entre 2 nombres du tirage
if (t[i]=operation(tab[i],tab[j],o))
{
//if (t[i]!=cible) continue;
//si on a trouvé, alors on affiche la solution
if(t[i]==cible)
{
strcpy(str_tmp,str_result);
sprintf(str_result,"%d %c %d = %d\n", tab[i],operateurs[o],tab[j],t[i]);
return printf("%s",str_result);
}
//sinon, on compare le resultat par rapport au nombre cible
else if (GAP(cible,t[i]) < best_gap)
{
best_tot=t[i];
best_gap=GAP(cible,best_tot);
}
//on retrie le tableau
t[i]=t[taille-1];
if (calculuser(cible,t, taille-1)) //appel récursif pr recommencer
{
strcpy(str_tmp,str_result);
sprintf(str_result,"%d %c %d = %d\n", tab[i],operateurs[o],tab[j],t[i]);
return printf("%s",str_result);
}
}
}
}
}
return;
}
//fonction qui calcule les opérations entre 2 nb tirés
int operation(int n1, int n2, int operateur)
{
switch (operateur)
{
case 0:
{
return (n1+n2);
break;
}
case 1:
{
return (n1-n2);
break;
}
case 2:
{
return GAP(n1,n2);
break;
}
case 3:
{
if (0== n1%n2) return (n1/n2);
break;
}
default:
{
printf("ERROR!!!\n");
break;
}
}
return 0;
} |
Partager