2
我發現一些代碼來獲得帕斯卡三角,而無需使用數組或nCr的在Java中,如下:背後的邏輯找出楊輝三角
int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++)
{
num = 1;
r = i + 1;
//pre-spacing
for (int j = maxRows - i; j > 0; j--)
{
System.out.print(" ");
}
for (int col = 0; col <= i; col++)
{
if (col > 0)
{
num = num * (r - col)/col;
}
System.out.print(num + " ");
}
System.out.println();
}
對我的生活中,我想不出如何的碼位產生所需數目(在序列中下一個):
for (int col = 0; col <= i; col++)
{
if (col > 0)
{
num = num * (r - col)/col;
}
System.out.print(num + " ");
}
可能有人請解釋後面是如何產生的數量的邏輯?我有興趣瞭解如何獲得下一位數字的公式,即num=num*(r-col)/col
如何工作!我也有興趣找出如何推導出這樣一個公式。
聽起來像https://en.wikipedia.org/wiki/Binomial_theorem二項式係數 – Toumash
@Toumash這裏使用的二項式定理究竟如何? –
可能的重複http://stackoverflow.com/questions/24294192/computing-the-binomial-coefficient-in-c/24294262#24294262 – Codor