zoukankan      html  css  js  c++  java
  • 杨辉三角的几种方法

    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]);
         }
         
         
         
         
         
         
         
        }
    
    }
  • 相关阅读:
    springboot之mybatis别名的设置
    webstorm
    万字长文把 VSCode 打造成 C++ 开发利器
    残差residual VS 误差 error
    参数与非参数的机器学习算法
    阿里云产品梳理
    aws产品整理
    Azure产品整理
    OpenStack产品摘要
    头条、美团、滴滴、阿里、腾讯、百度、华为、京东职级体系及对应薪酬
  • 原文地址:https://www.cnblogs.com/-strong/p/7157318.html
Copyright © 2011-2022 走看看