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
| #include <stdio.h>
#include <string.h>
int pieces[] = {1, 2, 5, 10, 20, 50, 100, 200};
char *nom[] = {"0,01", "0,02", "0,05", "0,10",
"0,20", "0,50", "1", "2"};
#define nbP (sizeof(pieces)/sizeof(pieces[0]))
int res[256][8];
int nbL;
void insert_resultat(int *t)
{
memcpy(res[nbL], t, sizeof(res[nbL]));
nbL ++;
}
void Calcule(int s, int ind, int *t)
{
int i = 0;
if (ind == 0)
{
t[ind] = s;
insert_resultat(t);
}
else
{
while (i * pieces[ind] <= s)
{
t[ind] = i;
Calcule(s - i * pieces[ind], ind - 1, t);
i++;
}
}
}
// affichage de la somme avec les pièces
void Affiche(void)
{
int i ;
for(i = 0; i < nbL; i++)
{
int j;
for( j = 0; j < nbP; j++)
printf("%3d\t", res[i][j]);
puts("");
}
}
int main(void)
{
int S = 17;
int t[8] = {0};
nbL = 0;
Calcule(S, nbP-1, t);
Affiche();
return 0;
} |
Partager