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
| #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 nbch);
int operation(int n1, int n2, int operateur);
int best_tot = 0, best_gap = 999;
char str_result[555],str_tmp[555];
int main()
{
int nbc, nbch=6; //nb cible
int nbtir[6];
//appel des fonctions
nbc=initialisation(nbtir);
if ( ! calculuser(nbc,nbtir,nbch) )
{
calculuser(best_tot,nbtir,nbch);
printf("Solution approchee : \n");
}
else printf("Le compte est bon \n");
printf("\n");
//affichage solution
//printf(str_result);
return 0;
}
//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++)
{
nouveau: //etiquette
indice= rand()%14;
for(j=0;j<6;j++)
{
if (nbtires[j]==nbpossible[indice]) goto nouveau; //condition pour ne pas avoir 2 fois le meme nombre
}
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[i]=operation(tab[i],tab[j],o);//t contient les résultat des opérations entre 2 nombres du tirage
//si on n'a pas encore trouvé, on continue de chercher
if (!t[i]) 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]);
// printf("%s",str_result);
printf("%d %c %d = %d\n", tab[i],operateurs[o],tab[j],t[i]);
return ;
}
//sinon, on compare le resultat par rapport au nombre cible
// if (GAP(cible,t[i]) < best_gap)
// {
// best_tot=t[i];
// best_gap=GAP(cible,best_tot);
// }
//on retrie le tableau
t[j]=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]);
// printf("%s",str_result);
printf("%d %c %d = %d\n", tab[i],operateurs[o],tab[j],t[i]);
return ;
}
}
}
}
return 0;
}
//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 GAP(n1,n2);
break;
}
case 2:
{
return (n1*n2);
break;
}
case 3:
{
if (0== n1%n2) return (n1/n2);
break;
}
default:
{
printf("ERROR!!!\n");
break;
}
}
return 0;
} |