嘿我有一个问题,我需要创建两个函数,countWithPerms()和ignorePerms()这两个函数必须是递归解决方案。 countWithPerms()会计算实际排列的数量,而ignorePerms()只会计算重复排列的数量。所以如果我将3传入函数countWithPerms()会发现3 =(2 + 1)=(1 + 2)=(1 + 1 + 2) 1),因此countWithPerms(3)为3,因为它计数3种方式来总结3.由于(1 + 2)和(2 + 1),countIgnorePerms(3)为2,因此它们都不会计入countWithPerms中是正好写在相反的顺序。排列的递归解决方案
大量例子是countWithPerms(7)是63,而countIgnorePerms(7)为14
我已经做countwithPerms,但我完全被卡住的countIgnorePerms。
int countWithPerms(int n)
{
if(n == 1)
return 0;
else
n--;
return (countWithPerms(n) + 1) +
(countWithPerms(n));
}
int ignorePerms(int sum, int xmin){
if(sum == 1)
return 0;
else
for(int i=0; i<sum;i++){
sum += sum-xmin;
2*ignorePerms(sum,xmin)+1;
return sum;
}
}
你正在计数分区,而不是排列。 – aschepler
ignorePerms()被称为[整数分区](http://en.wikipedia.org/wiki/Partition_(number_theory))和countWithPerms()被称为[整数组成](http://en.wikipedia.org/维基/ Composition_(number_theory))。 – Ante
我想你是在排列的地方计算操作。 – Shravan40