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
|
#include <stdio.h>
#include <string.h>
#define MAX 1024
// on cherche à tro4 objets pris parmi 3
int NB;
void ecrire(int *t, int l,FILE*fichier) {
int i,j;
for (i = 0; i < l; ++i)
fprintf(fichier,"%d ",t[i]);
for (i = 0; i < l; ++i) {
for (j = 0; j < t[i]; ++j) {
}
}
fprintf(fichier,"\n");
++NB;
}
void afficher(int *t, int l,FILE*fichier) {
int i,j;
for (i = 0; i < l; ++i)
printf("%d ",t[i]);
for (i = 0; i < l; ++i) {
for (j = 0; j < t[i]; ++j) {
}
}
printf("\n");
++NB;
}
void kcombinaison(int *t, int n, int p, void (*afficher)(int*,int,FILE*fichier), FILE*fichier) {
int tmp,i;
t[0] = p;
memset(t+1,0, sizeof *t * (n - 1));
while(t[n-1] != p) {
afficher(t,n, fichier);
tmp = t[n-1];
t[n-1] = 0;
i = n-1;
while(t[i] == 0) --i;
--t[i];
t[i+1] = tmp + 1;
}
afficher(t,n, fichier);
//fprintf(fichier,"%d",t[i]);
}
int main() {
int tab[MAX];
int n = 3, p = 4; // 4 objets pris parmi 3
NB = 0;
FILE *fichier=fopen("Combinations.txt","w");
if(fichier==NULL)
{
printf("Failed to open Combinations file, quitting!\n");
return 1;
}
//kcombinaison(tab,n,p,&afficher,fichier);
kcombinaison(tab,n,p,&ecrire,fichier);
fclose(fichier);
return 0;
} |
Partager