标签:
2 3
1 1 1 1 1 1 1 2 1
import java.util.Scanner; public class Main { static int[] mat = new int[31]; public static void main(String args[]) { Scanner sc = new Scanner(System.in); mat[0] = 1;// 初始化第一个元素,也就是第一列的值全为1; int n = 0; while (sc.hasNext()) { n = sc.nextInt(); triangle(n); } } public static void triangle(int n) { System.out.println(mat[0]); mat[n - 1] = 1;// 从后面往前面 for (int i = 1; i < n; i++) {// 控制行数 mat[i] = 1; for (int j = i - 1; j > 0; j--) { mat[j] = mat[j] + mat[j - 1]; if (mat[j] < 0) { return; } } for (int j = 0; j <= i; j++) { if (j == 0) { System.out.print(mat[j]); } else { System.out.print(" " + mat[j]); } } System.out.println(); } System.out.println(); } }
标签:
原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46408971