利用数组显示杨辉三角

来源:互联网 发布:淘宝助手客服电话 编辑:程序博客网 时间:2024/05/22 17:17
package com.lanou.java02;

import java.util.Scanner;

public class Homework1 {
    public static void main(String[] args) {
        
        /*
         * 1、首先我要一个二维数组来显示杨辉三角
         * */
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int [][] yh= new int[row][];
        for(int i=0;i<yh.length;i++){
            int[] col = new int[i+1];
            col[0]=1;
            col[i]=1;
            if(i>=2){
                for(int j=1;j<i;j++){
                    col[j]=yh[i-1][j-1]+yh[i-1][j];                    
                }
            }
            yh[i]=col;
        }
        for(int i=0;i<yh.length;i++){
            for(int j=0;j<yh[i].length;j++){
                System.out.print(yh[i][j]+" ");
            }
            System.out.println();
        }
    }
}   
原创粉丝点击