Compte est bon en C
basma smi
Messages postés
1
Date d'inscription
Statut
Membre
Dernière intervention
-
NESRINE -
NESRINE -
bsr à tous bon voila un programme de "le compte est bon" en C je ve savoir est ce que ca marche:
#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(1);
}
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);
}
}
}
merci
#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(1);
}
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);
}
}
}
merci
A voir également:
- Compte est bon en C
- Créer un compte google - Guide
- Installer windows 10 sans compte microsoft - Guide
- Créer un compte gmail - Guide
- Comment savoir qui regarde mon compte facebook - Guide
- Créer un compte le bon coin - Guide
3 réponses
Salut,
Beh, oui, il a l'air de marcher. Après, il y a pleins de warnings à corriger. Par contre, je n'ai pas regardé ton algorithme, car sans commentaires...
Du coup en me servant de ton programme, je réponds à la question de coline :
330 est impossible à atteindre. Mais tu peux tomber sur 329.
7*8 = 56.
56-9=47.
4+3=7.
47*7=329.
Cdt
Beh, oui, il a l'air de marcher. Après, il y a pleins de warnings à corriger. Par contre, je n'ai pas regardé ton algorithme, car sans commentaires...
Du coup en me servant de ton programme, je réponds à la question de coline :
330 est impossible à atteindre. Mais tu peux tomber sur 329.
7*8 = 56.
56-9=47.
4+3=7.
47*7=329.
Cdt