数组问题------杨辉三角

//从键盘上输入一个正整数n,请按照以下五行杨辉三角形的显示方式,
//输出杨辉三角形的前n行。请采用循环控制语句来实现。
//(三角形腰上的数为1,其他位置的数为其上一行相邻两个数之和。)
//1
//1   1
//1   2   1
//1   3   3   1
//1   4   6   4   1 
//1   5   10  10  5  1

package 数组;

import java.util.Scanner;

public class 杨辉三角
{

    public static void main(String[] args)
    {

        int i ;
        int j ;
        int n ;
        Scanner sc = new Scanner ( System.in ) ;
        System.out.println( "请输入行数n: " ) ;
        n = sc.nextInt() ;
        int [ ] [ ] array = new int [ n ] [ n ] ;

        for ( i = 0 ; i < n ; i++ )
        {
            for ( j = 0 ; j <= i ; j++ )
            {
                if ( j == 0 || j == n )
                {
                    array [ i ] [ j ] = 1 ;
                }
                else
                {
                    array [ i ] [ j ] = array [ i - 1 ] [ j - 1 ] + array [ i - 1 ] [ j ] ;
                }
                System.out.print( array [ i ] [ j ] + " " ) ;
            }
            System.out.println( ) ;
        }
        
    }

}

原文地址:https://www.cnblogs.com/20gg-com/p/5876399.html