questo è il codice C trovato :
codice:
#include<stdio.h>
#include<stdlib.h>
#define false 0;
#define true 1;
#define Min(x,y) ((x<y)?x:y)
typedef struct partitionnode
{
int m;
int numparts;
int parts[101];
} partition;
void output(partition a)
/*
** print out the partition a
*/
{
int i,j;
printf("%d = ",a.m);
for(i=1;i<=a.numparts;i=i+1)
{
j = a.parts[i];
printf("%d",j);
if (i != a.numparts)
printf("+");
}
}
void PartitionLexSuccessor(int m,int n,partition *a,int *flag)
/*
** Algorithm 3.7
** replaces the partition a by its successor,
** where a is given in standard form
*/
{
int i,j,d;
i = 2;
while( (i<=n) && ((*a).parts[1]<=((*a).parts[i]+1)) )
i=i+1;
if(i == (n+1))
{
*flag = false;
}
else
{
(*a).parts[i] = (*a).parts[i] + 1;
d = -1;
for(j=(i-1);j>=2;j=j-1)
{
d = d + (*a).parts[j] - (*a).parts[i];
(*a).parts[j] = (*a).parts[i];
}
(*a).parts[1] = (*a).parts[1] + d;
}
}
int main()
{
int m,n,i,j,r,s,num;
int flag;
partition a;
char junk;
m = 10;
n = 4;
printf("Test of Algorithm 3.7 with m=%d n=%d \n\n",m,n);
a.m = m;
a.numparts = n;
a.parts[1] = m-n+1;
for(i=2;i<=n;i=i+1)
a.parts[i] = 1;
flag = true;
while(flag)
{
output(a);
PartitionLexSuccessor(m,n,&a,&flag);
}
printf("\nEnd of test. \n");
return(0);
}
la variabile m dichiarata in main equivale al risultato e
la variabile n dichiarata in main equivale al numero degli addendi.
compilato e provato . funziona.
siccome devo tradurla in linguaggio PHP fino ad un certo punto riesco ad intepretarlo, ma ci sono alcuni passaggi che non mi sono chiari
come ad esempio, cosa sta a significare questo codice :
codice:
typedef struct partitionnode
{
int m;
int numparts;
int parts[101];
} partition;
e soprattutto la dichiarazione "partition a" posta nella funzione main, e richiamata nelle altre funzioni.
qualcuno ha dimestichezza con il C ?