jeudi 5 mars 2015

counting and printing partitions of 1 to n-1


I am trying write a recursive function(it must be recursive) to print out the partitions and number of partitions for 1 to n-1. For example, 4 combinations that sum to 4:



1 1 1 1
1 1 2
1 3
2 2


I am just having much trouble with the function. This function below doesn't work. Can someone help me please?



int partition(int n, int max)
{

if(n==1||max==1)
return(1);
int counter = 0;
if(n<=max)
counter=1;
for(int i = 0; n>i; i++){
n=n-1;
cout << n << "+"<< i <<"\n";
counter++;
partition(n,i);
}

return(counter);
}



Aucun commentaire:

Enregistrer un commentaire