(1)形如
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
称为杨辉三角
详细的概念参考:
http://baike.baidu.com/link?url=UO76y1CzNbuy2qZggVZwuoI6MNinSV_YFCpLmJ55aIl83ufTK5CGoOyBV9kg23h_maf13KLrf7rLmGjkD8lmmDn0tKP4-PIPosuzk3YMOqsCsn90GQKtDjwbD4umz95S
(2)实现(涉及的概念:任何一个数字等于它肩膀上的两个数字之和)
import java.util.Scanner; /** * @date 2017-5-29 * @author liuffei * @description 杨辉三角实现 */ public class YangHuiTriangle { public static int[][] yangHuiTriangle(int n){ //建立杨辉三角二维矩阵 int triangle[][] = new int[n][]; if(n >= 1){ /** * 1 */ triangle[0] = new int[]{1}; } if(n >= 2){ /** * 1 * 1 1 */ triangle[0] = new int[]{1}; triangle[1] = new int[]{1,1}; } if(n >= 3){ /** * 1 * 1 1 * 1 2 1 * 1 3 3 1 * 1 4 6 4 1 */ for(int i = 2;i < n;i++){ int row[] = new int[i+1]; row[0] = 1;//杨辉三角每行的第一个数都是1 for(int j = 1;j < i;j++){ //中间数的值等于它肩膀上两个数的和 row[j] = triangle[i-1][j-1] + triangle[i-1][j]; } row[i] = 1;//杨辉三角每行的最后一个数也是1 triangle[i] = row; } } return triangle; } public static void main(String[] args) { System.out.println("请输入杨辉三角整数n="); //命令行输入值 Scanner sc = new Scanner(System.in); int input = sc.nextInt(); int triangle[][] = yangHuiTriangle(input); //打印 for(int i = 0;i < triangle.length;i++){ for(int j = 0;j < triangle[i].length;j++){ System.out.print(triangle[i][j] + " "); } System.out.println(""); } } }