我想通过给方法提供行号和列号来找出Pascal三角形的正确数字。Pascal的三角Java实现
我发现这段代码:
public static int getPascalValue(int row, int column) {
if (column == 0 || column == row) {
return 1;
}
return getPascalValue(row - 1, column - 1) + getPascalValue(row - 1, column);
}
public static void main(String args[]) {
System.out.println("\r\n"+getPascalValue(3,2));
}
这似乎是正确的我,但给出了错误的号码。
例如主要应导致2号,但它给了我3,因为:
1
1 1
1 2 1
1 3 3 1
我想不出错递归的一部分,任何人可以帮助我吗?
谢谢!
也许你只是困惑于它是基于零还是基于一个? –
只需将'if'更改为'column == 1',以使输入1为基础。 – 4castle
正如我确信您知道的那样,我们是一个问答网站,而不是编码人员出租服务。请解释你到目前为止尝试过的以及为什么它没有奏效。请参阅:[为什么“有人可以帮助我?”不是一个实际的问题?](http://meta.stackoverflow.com/q/284236)和[当我不确定我在找什么时问一个好问题?](https:// meta。 stackoverflow.com/questions/262527/how-to-ask-a-good-question-when-im-not-sure-what-im-looking-for) –