开发者

n Choose k using an integer type of n and k in java

I have been given a task that requires to use n and k to work out the n Choose k problem. The condition I have is I can not use other data type other than int. I'm not be able to use the package such as BigInteger.

The int n and k can be any number below 200. How can I avoid the number grows too large? Because the program blows when n > 20.

Thanks.

Wikipedia: "n Choose k" or "Binomial coeffi开发者_运维问答cient"


use recursion relation: (n, k) = (n-1,k-1)+(n-1,k)

base cases: (n, 0) = 1; (0, k) = 0


I think the problem requires you to create your own data-structure that can hold huge numbers or alteast represent huge numbers. Once that is done the rest is trivial. You might want to look at the source code of BigInteger to see how they do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜