1089: 杨辉三角

来源:互联网 发布:古生物学与地层学知乎 编辑:程序博客网 时间:2024/06/08 16:47

网址:http://begin.lydsy.com/JudgeOnline/problem.php?id=1089
1089: 杨辉三角
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 480 Solved: 176
[Submit][Status][Web Board]
Description
输出杨辉三角的前N行(N<10)。
Input
输入只有一行,包括1个整数N。(N<10)
Output
输出只有N行,每行输出的数字间用一个空格分开,最后一个数字后面没有空格。
Sample Input
5
Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
HINT
Source
AC代码:
package 杨辉三角0089;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {     Scanner in = new Scanner(System.in);     int lines = in.nextInt();     printYFTriangle(lines);}public static void printYFTriangle(int lines){    int[] a = new int[lines + 1];    int previous = 1;    for (int i = 1; i <= lines; i ++){        for (int j = 1; j <= i; j++){            int current = a[j];            a[j] = previous + current;            previous = current;            if (j == i) {                System.out.print(a[j]);            } else {                System.out.print(a[j] + " ");            }        }        System.out.println();    }}

}