首先,很難看到Hosoya三角形Wiki頁面提供的格式。我們採取的第一個5行:
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
,並重新安排他們看起來像這樣:
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
,你也許可以看到現在的格局:
starting from the 3rd row:
for every number in the row
if the number has a number above it (i.e. all except the last number in each row)
it's the sum of the two numbers straight above it: H(n,j) = H(n-1,j) + H(n-2,j)
otherwise (i.e. the last number in each row)
it's the sum of the two numbers above it in the left diagonal: H(n,j) = H(n-1,j-1) + H(n-2),j-2)
重新格式化的數字可以存儲在二維數組中,如圖所示。那麼我們需要做的就是這樣它看起來像演示的Wiki頁面上顯示有適當的空間將其打印出來:
public class HosoyaTriangle {
public static void main(String args[]) {
final int N = 10;
int[][] triangle = new int[N][N]; // this would initialize all cell elements to be 0
//populate the base cases for the first two rows
//H(0,0) = H(1,0) = H(1,1) = 1
triangle[0][0] = triangle[1][0] = triangle[1][1] = 1;
//starting from the 3rd row
for (int row = 2; row < N; row++) {
for (int col = 0; col < N; col++) {
if (col < row) {
//H(n,j) = H(n-1,j) + H(n-2,j)
triangle[row][col] = triangle[row - 1][col] + triangle[row - 2][col];
} else {
//H(n,j) = H(n-1,j-1) + H(n-2),j-2)
triangle[row][col] = triangle[row - 1][col - 1] + triangle[row - 2][col - 2];
}
}
}
print(triangle);
}
private static void print(int[][] matrix) {
final int level = matrix.length;
int spaceCount;
StringBuilder sb;
for (int row = 0; row < level; row++) {
sb = new StringBuilder();
//figure out how many spaces need to be printed before
//printing out the first non-zero number in the row
spaceCount = level - row - 1;
//add the spaces
while(spaceCount-- > 0) {
sb.append(" ");
}
//add all the non-zero numbers in the row
for (int col = 0; col < level; col++) {
if (matrix[row][col] > 0) {
sb.append(String.format("%4d",matrix[row][col]));
}
}
System.out.println(sb.toString());
}
}
}
輸出:
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
8 5 6 6 5 8
13 8 10 9 10 8 13
21 13 16 15 15 16 13 21
34 21 26 24 25 24 26 21 34
55 34 42 39 40 40 39 42 34 55
編輯:
意識到您正在尋找遞歸解決方案。給定每個數字是由數字中的行計算上述情況,我們可以採用斐波納契序列的相同的邏輯和從第N行開始,並遞歸地向上傳播的util我們打基例:
public static void main(String args[]) {
final int N = 10;
int[][] triangle = new int[N][N]; // this would initialize all cell elements to be 0
//only need to loop through the last row
//each column is calculated as a separate fibonacci sequence
for (int col = N - 1; col >= 0; col--) {
calc(N - 1, col, triangle);
}
print(triangle);
}
private static int calc(int row, int col, int[][] triangle) {
//base cases
if (row == 0 && col == 0 || row == 1 && col == 0 || row == 1 && col == 1 || row == 2 && col == 1) {
triangle[row][col] = 1;
} else {
if (col < row) {
//H(n,j) = H(n-1,j) + H(n-2,j)
triangle[row][col] = calc(row - 1, col, triangle) + calc(row - 2, col, triangle);
} else if (col == row) {
//H(n,j) = H(n-1,j-1) + H(n-2),j-2)
triangle[row][col] = calc(row - 1, col - 1, triangle) + calc(row - 2, col - 2, triangle);
}
}
return triangle[row][col];
}
注意,此溶液比非遞歸的要慢得多。
非常感謝你!你基本上拯救了我的生命哈哈 我有一個問題與StringBuilder有關,我是一個初學者,我沒有真正與字符串格式化那麼多..所以我的問題是'%4d'究竟是什麼意思?如果它沒有太多的麻煩,你是否可以回過頭來用StringBuilder在整個'print'方法中添加註釋,並解釋發生了什麼?我在互聯網上的其他地方几乎找不到任何有意義的東西 –
'%4d'告訴格式化器將輸入格式化爲一個** d **的極小整數,最小** 4 **個字符的數字要寫入輸出。我這樣做是爲了讓輸出看起來更好。你可以在這裏找到更多的細節(https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html)。 – whoisdan