打印杨辉三角

【题目要求】

打印输出一个8行8列的杨辉三角

public class Dem12 {
	/**
	 * 实现杨辉三角
	 * @param args
	 */
	public static void main(String[] args) {
		int p[][] = new int[8][8];
		
		for(int i=0;i<8;i++) {
			for(int j=0;j<=i;j++) {
				if(j==0 || j==i) {
					p[i][j] = 1;
				}else {
					p[i][j] = p[i-1][j] + p[i-1][j-1]; 
				}
			}
		}
		
		for(int i=0;i<8;i++) {
			for(int j=0;j<=i;j++) {
				System.out.print(p[i][j]+" ");
			}
			System.out.println();
		}
		
	}

}

【输出结果】


1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 


猜你喜欢

转载自blog.csdn.net/wyf2017/article/details/80329164