[BASIC-6]-杨辉三角形-(java)

最好自己想,不要去参考网上的答案,纯找规律题

import java.util.Scanner;

public class B_06 {

	public static void main(String[] args) {
		int arr[][] = new int[34][34];
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		for (int i = 0; i < n; ++i) {
			arr[i][0] = 1;
			for (int j = 0; j < n; ++j) {
				if (i == j)
					arr[i][i] = 1;
			}
		}
		for(int i=2; i<n; ++i){
			for(int j=1; j<i; ++j){
				arr[i][j] = arr[i-1][j-1]+arr[i-1][j];
			}
		}
		for(int i=0; i<n; ++i)
		{
			for(int j=0; j<arr[i].length; j++){
				if(arr[i][j]==0)
					break;
				if(j==0)
					System.out.print(arr[i][j]);
				else
					System.out.print(" "+arr[i][j]);
			}
			System.out.println();
		}
	}

}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/sinat_32588261/article/details/63255304
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞