杨辉三角的几种方法

import java.util.Scanner;

public class YH {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根

//1 
//1 1 
//1 2 1 
//1 3 3 1 
//1 4 6 4 1 
//1 5 10 10 5 1 
//1 6 15 20 15 6 1 
//1 7 21 35 35 21 7 1 
//1 8 28 56 70 56 28 8 1 
//1 9 36 84 126 126 84 36 9 1
        System.out.println("请问您需要输入几行:");
        Scanner sc =new Scanner (System.in);    
        int a = sc.nextInt();
        
        int [][] b=new int[a][a];
        for(int x=0;x<b.length;x++){
            for(int y=0;y<b[x].length;y++){
                b[x][y]=1;
            }
        }
        for(int x=2;x<b.length;x++){
            for(int y=1;y<x;y++){
                b[x][y]=b[x-1][y-1]+b[x-1][y];
            }
            System.out.println();
        }
        for(int x=0;x<b.length;x++){
            for(int y=0;y<=x;y++){
                System.out.print(b[x][y]);
                System.out.print(" ");
            }
            System.out.println();
        }                                                                        
    }
    }
import java.util.Scanner;

public class YH1 {
//郑威威
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
     System.out.print("请输入需要的行数:");    
     Scanner sc=new Scanner(System.in);
     int a =sc.nextInt();
     int [][]b=new int [a][a];
     b[0][0]=1;
     b[1][0]=1;
     b[1][1]=1;
     System.out.println(b[0][0]);
     System.out.println(b[1][0]+" "+b[1][1]);
     
     for(int x =2;x< b.length ;x++){
         
         b[x][0]=1;
         
         System.out.print(b[x][0]+" ");
         
         for(int y=1;y<=x-1;y++){
             
            b[x][y]=b[x-1][y-1]+b[x-1][y];
            
            System.out.print(b[x][y]+" ");
         }
         
         b[x][x]=1;
         
         System.out.println(b[x][x]);
     }
     
     
     
     
     
     
     
    }

}
原文地址:https://www.cnblogs.com/-strong/p/7157318.html