java在编辑三角形,java - 在java中创建一个Hosoya的三角形 - 堆栈内存溢出

首先,使用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)

重新格式化的数字可以存储在2D阵列中,如图所示。 然后我们需要做的就是用适当的空格打印出来,这样看起来像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

编辑:

意识到你正在寻找递归解决方案。 给定每个数字由上面的行中的数字计算,我们可以使用相同的fibonacci序列逻辑并从第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];

}

请注意,此解决方案比非递归解决方案慢得多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值