Demo entry 6644059

a

   

Submitted by anonymous on Sep 30, 2017 at 11:02
Language: Java. Code size: 759 Bytes.

import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		System.out.println("输入要打印的行数");
		Scanner sc=new Scanner(System.in);
		int row = sc.nextInt();
		//声明二维数组
		int[][] arr=new int[10][];
		for (int i = 0; i < arr.length; i++) {
			arr[i]=new int[i+1];	
		}
		//代码实现
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				if (j==arr[i].length-1 || j==0 ) {//如果是数字第一个元素或者最后一个元素,值为1
					arr[i][j]=1;
				}else {
					arr[i][j]=arr[i-1][j]+arr[i-1][j-1];//否则这个元素就是左上与正上元素之和
				}
			}
		}
		//遍历输出
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j]+"  ");
			}
			System.out.println();
		}
	}
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).