【发布时间】:2011-11-06 07:34:08
【问题描述】:
我有一个小程序。我必须计算重复的组合。
我的代码:
int factorial(int a){
if (a<0)
return 0;
if (a==0 || a==1)
return 1;
return factorial(a-1)*a;
}
long int combinationWithRepetion(int n, int k){
long int a,b,wyn=0;
wyn=factorial(n+(k-1))/(factorial(k)*factorial(n-1));
return wyn;
}
int main()
{
int k,n=0;
cout<<"Give n: ";
cin>>n;
cout<<"Give k: ";
cin>>k;
cout<<"combination With Repetion for n="<<n<<
" k="<<k<<".\n Is equal to "<<combinationWithRepetion(n,k)<<endl;
return 0;
}
对于 Wolfram alfa 中的 n=9 和 k=6,我得到 3003,但在这个程序中,结果是 44。
对我来说,代码很好。
【问题讨论】:
-
你会信任谁? Wolfram Alpha 还是你的代码?
-
@MitchWheat,显然是他自己的代码!
-
@MitchWheat 但是你对代码的看法,也许我错过了什么?
标签: c++ math combinations