Accueil > > > LE COMPTE EST BON
LE COMPTE EST BON
Information sur la source
Description
Ce programme donne la solution au jeu "Le compte est bon" Avec 6 nombres et les 4 operations le programme doit obtenir un 7e nombre defini à l'avance. Il fonctionne avec 2 modes, un mode automatique choisissant aleatoirement 6 nombre dans une liste de 13, et un resultat à obtenir compris entre 100 et 999. Le mode personnalisé donne la possibilitié à l'utilisateur de donner lui meme les nombres et le résultat. En cas de solution impossible le code tente pour le nombre n-1. Pour resoudre l'operation le programme utilise une simple reccursivité.
Source
- #include <stdio.h>
- #include <stdlib.h>
-
- #define N 13
- #define M 6
- #define OP 4
-
-
-
-
- char operateur[] = "+*/-";
- int compte(int *tab, int nombre, int total,long *compteur)
- {
-
- int i,j,k,t[M];
- *compteur= *compteur +1;
-
-
- for ( i=0 ; i<nombre-1 ; i++ ) {
- for ( j=i+1 ; j<nombre ; j++) {
- for ( k=1; k<=OP; k++) {
- memcpy(t,tab,sizeof(int)*6);
-
- switch (k)
- {
-
- case 1:
- t[i]+=t[j];
- if (t[i] == total && nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]); return 1; }
- if (nombre>0)
- t[j]=t[nombre-1];
- if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1;}
- break;
-
- case 2:
- t[i]*=t[j];
- if (t[i] == total && nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
- if (nombre>0)
- t[j]=t[nombre-1];
- if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
- break;
-
- case 3:
- if ( (t[i]>t[j] && t[i]> 0 && t[j] > 0 && ((t[i]%t[j])==0)) ) {t[i] = t[i]/t[j]; } else break;
- if (t[i] == total && nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
- if (nombre>0)
- t[j]=t[nombre-1];
- if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
- break;
-
- case 4:
- if (t[i]<t[j]) { t[i] = t[j]-t[i];}
- else t[i]-=t[j];
- if (t[i] == total&& nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]); return 1; }
- if (nombre>0)
- t[j]=t[nombre-1];
- if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]); return 1; }
- break;
-
- }
- }
- }
- }
-
- return 0;
- }
-
-
-
-
- int main(void)
- {
-
- int depart[N],nombre[M],i=0,tranz,total,oui=3;
- long compteur=0;
- /* initialisation de la fonction rand() */
- srand(time(NULL ));
-
- /* Tableau de depart */
- depart[0]=1;
- depart[1]=2;
- depart[2]=3;
- depart[3]=4;
- depart[4]=5;
- depart[5]=6;
- depart[6]=7;
- depart[7]=8;
- depart[8]=9;
- depart[9]=10;
- depart[10]=25;
- depart[11]=50;
- depart[12]=100;
- /* Fin tableau de depart */
-
- for (i=0;i<7;i++)
- {
- nombre[i]=0;
- }
-
- while (oui==3)
- {
- printf("Choisissez le mode appropriZ:\n (1) mode personnalisZ (2) mode automatique (4) sortir\n");
- scanf("%d",&oui);
-
- while (oui==2)
- {
-
- total = rand()%899+100;
- i=0;
- while (i<7)
- {
- compteur=0;
- tranz = depart[rand() % 13];
- if (( tranz == nombre[0] ) || (tranz == nombre[1]) || (tranz ==
- nombre[2]) ||(tranz == nombre[3]) ||(tranz == nombre[4]) || (tranz==nombre[5]))
- {
- exit;
- }
- else
- {
- nombre[i]= tranz;
- i++;
- }
- }
-
-
- printf ("Le nombre a atteindre est: %d\nAvec les nombres : ",total);
- for (i=0;i<6;i++)
- {
- printf(" %d ", nombre[i]);
- }
- printf ("\nEt les operateurs: %c %c %c %c \n",operateur[0],operateur[1],operateur[2],operateur[3]);
-
-
- while (compte(nombre,M,total,&compteur)==0)
- {
- total=total-1;
- printf("Le compte exact est impossible ^ trouver la solution qui se rapproche le plus est: %d\n",total);
-
- }
-
- printf ("\nTapez (2) pour continuer (3) pour changer de mode\n");
- scanf("%d",&oui);
-
- }
-
-
- while (oui==1)
- {
- compteur =0;
- printf("Inscrivez les 7 nombres sous le format a b c d e f g total\n");
- scanf("%d %d %d %d %d %d %d",&nombre[0],&nombre[1],&nombre[2],&nombre[3],&nombre[4],&nombre[5],&total);
-
-
- printf ("Le nombre a atteindre est: %d\nAvec les nombres : ",total);
- for (i=0;i<6;i++)
- {
- printf(" %d ", nombre[i]);
- }
- printf ("\nEt les operateurs: %c %c %c %c \n",operateur[0],operateur[1],operateur[2],operateur[3]);
-
-
- while (compte(nombre,M,total,&compteur)==0)
- {
- total=total-1;
- printf("Le compte exact est impossible ^ trouver la solution qui se rapproche le plus est: %d\n",total);
-
- }
-
- printf ("\nTapez (1) pour continuer (3) pour changer de mode\n");
- scanf("%d",&oui);
-
- }
-
- }
-
-
- }
#include <stdio.h>
#include <stdlib.h>
#define N 13
#define M 6
#define OP 4
char operateur[] = "+*/-";
int compte(int *tab, int nombre, int total,long *compteur)
{
int i,j,k,t[M];
*compteur= *compteur +1;
for ( i=0 ; i<nombre-1 ; i++ ) {
for ( j=i+1 ; j<nombre ; j++) {
for ( k=1; k<=OP; k++) {
memcpy(t,tab,sizeof(int)*6);
switch (k)
{
case 1:
t[i]+=t[j];
if (t[i] == total && nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]); return 1; }
if (nombre>0)
t[j]=t[nombre-1];
if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1;}
break;
case 2:
t[i]*=t[j];
if (t[i] == total && nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
if (nombre>0)
t[j]=t[nombre-1];
if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
break;
case 3:
if ( (t[i]>t[j] && t[i]> 0 && t[j] > 0 && ((t[i]%t[j])==0)) ) {t[i] = t[i]/t[j]; } else break;
if (t[i] == total && nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
if (nombre>0)
t[j]=t[nombre-1];
if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]);return 1; }
break;
case 4:
if (t[i]<t[j]) { t[i] = t[j]-t[i];}
else t[i]-=t[j];
if (t[i] == total&& nombre==2) { printf("\n Le compte est bon! En %d opérations %\n",*compteur); printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]); return 1; }
if (nombre>0)
t[j]=t[nombre-1];
if (compte(t,nombre-1,total,&*compteur)) { printf("\n %3d %3c %3d = %3d \n",tab[i],operateur[k-1],tab[j],t[i]); return 1; }
break;
}
}
}
}
return 0;
}
int main(void)
{
int depart[N],nombre[M],i=0,tranz,total,oui=3;
long compteur=0;
/* initialisation de la fonction rand() */
srand(time(NULL ));
/* Tableau de depart */
depart[0]=1;
depart[1]=2;
depart[2]=3;
depart[3]=4;
depart[4]=5;
depart[5]=6;
depart[6]=7;
depart[7]=8;
depart[8]=9;
depart[9]=10;
depart[10]=25;
depart[11]=50;
depart[12]=100;
/* Fin tableau de depart */
for (i=0;i<7;i++)
{
nombre[i]=0;
}
while (oui==3)
{
printf("Choisissez le mode appropriZ:\n (1) mode personnalisZ (2) mode automatique (4) sortir\n");
scanf("%d",&oui);
while (oui==2)
{
total = rand()%899+100;
i=0;
while (i<7)
{
compteur=0;
tranz = depart[rand() % 13];
if (( tranz == nombre[0] ) || (tranz == nombre[1]) || (tranz ==
nombre[2]) ||(tranz == nombre[3]) ||(tranz == nombre[4]) || (tranz==nombre[5]))
{
exit;
}
else
{
nombre[i]= tranz;
i++;
}
}
printf ("Le nombre a atteindre est: %d\nAvec les nombres : ",total);
for (i=0;i<6;i++)
{
printf(" %d ", nombre[i]);
}
printf ("\nEt les operateurs: %c %c %c %c \n",operateur[0],operateur[1],operateur[2],operateur[3]);
while (compte(nombre,M,total,&compteur)==0)
{
total=total-1;
printf("Le compte exact est impossible ^ trouver la solution qui se rapproche le plus est: %d\n",total);
}
printf ("\nTapez (2) pour continuer (3) pour changer de mode\n");
scanf("%d",&oui);
}
while (oui==1)
{
compteur =0;
printf("Inscrivez les 7 nombres sous le format a b c d e f g total\n");
scanf("%d %d %d %d %d %d %d",&nombre[0],&nombre[1],&nombre[2],&nombre[3],&nombre[4],&nombre[5],&total);
printf ("Le nombre a atteindre est: %d\nAvec les nombres : ",total);
for (i=0;i<6;i++)
{
printf(" %d ", nombre[i]);
}
printf ("\nEt les operateurs: %c %c %c %c \n",operateur[0],operateur[1],operateur[2],operateur[3]);
while (compte(nombre,M,total,&compteur)==0)
{
total=total-1;
printf("Le compte exact est impossible ^ trouver la solution qui se rapproche le plus est: %d\n",total);
}
printf ("\nTapez (1) pour continuer (3) pour changer de mode\n");
scanf("%d",&oui);
}
}
}
Conclusion
Le code n'est pas vraiment optimisé, et le mode personnalisé est un peu trop libre, ce qui laisse à l'utilisateur la possibilité de faire a peu pres n'importe quoi. Si aucune solution n'est trouvée le programme se relance ce qui fait que sur une machine lente, le calcul peut prendre du temps.. A part ça ça doit marcher!!
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOURTECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOUR par ROMELARD Fabrice
Après un retour sur l'histoire des TechDays de Paris et le fait que ce soit le plus gros event MS au monde (du fait de sa gratuité), le président de MS France (Eric Boustoullier) a fait une présentation de la vision Microsoft pour les années à venir...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|