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
| #include <stdio.h>
#include <stdlib.h>
typedef unsigned short mtypeL;
typedef unsigned char mtypeS;
// estimation de la taille necessaire pour stocker la chaine suivante
mtypeL compter(mtypeS *ch, mtypeL ln)
{
mtypeL l=1, i=0;
mtypeS c=ch[0];
while (++i<ln)
if ( c!=ch[i] )
{
c = ch[i];
l++;
}
return 2*l;
}
// Lis la chaine src et stock le resultat dans *dest
void traiter(mtypeS *src, mtypeS **dest)
{
mtypeL lgs=0,lgd,i,j;
mtypeS c,k;
while (src[lgs++])
{}
lgd=compter(src,lgs);
free(*dest);
*dest=NULL;
if ( (*dest=malloc(lgd*sizeof(mtypeS)))==NULL )
return;
for (i=1,c=src[0],k=1,j=0; i<lgd; k++,i++)
if ( src[i]!=c )
{
(*dest)[j++]=k;
(*dest)[j++]=c;
c=src[i];
k=0;
}
(*dest)[j++]=1;
(*dest)[j]=0;
}
// Itere le traitement jusqu'a atteindre le rang souhaité
void trouver(mtypeL n)
{
mtypeS *t[2]={0},i;
t[0]=malloc(sizeof(mtypeS));
t[0][0]=0;
for (i=1; i<n; i++)
{
traiter(t[(i+1)%2],t+i%2);
if ( t[i%2] == NULL)
{
printf("ERREUR\n");
return;
}
}
i=0;n--;
do
printf("%d ",t[n%2][i]);
while ( t[n%2][i++] );
}
int main(void)
{
trouver(8);
getchar();
return(0);
} |
Partager