蓝桥杯——杨辉三角
代码
package day3;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//for (int i = 0; i < 10; i++) {
//for (int j = 0; j < 10; j++) {
//System.out.print("外:"+i+"内:"+j);
//}
//System.out.println();
//}
int n = 6;
int [][] arr = new int [n+1][n+1];
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j <=i; j++) {
if(i==1||i==j) {
arr[i][j]=1;
}else {
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
}
}
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(arr[i][j]+",");
}
System.out.println();
}
}
}
i内层,j外层
核心:每个位置的值为(i-1,j)位置的值加(i-1,j-1)位置的值
运行结果
原文地址:https://blog.csdn.net/bengbeng2345/article/details/143772120
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!