zoukankan      html  css  js  c++  java
  • Java-数组练习5

    5.从键盘上输入一个正整数n,请按照以下五行杨辉三角形的显示方式,

    输出杨辉三角形的前n行。请采用循环控制语句来实现。

    (三角形腰上的数为1,其他位置的数为其上一行相邻两个数之和。)

    1

    1   1

    1   2   1

    1   3   3   1

    1   4   6   4   1

    1   5   10  10  5  1

     

    Scanner sc=new Scanner(System.in);

          System.out.println("输入一个数打印杨辉三角:");

          int t=sc.nextInt();

           int[][] array = new int[t][t];

               for(int i=0;i<array.length;i++)

               {

                   for(int j=0;j<=i;j++){

                       if(j==0 || j==i){

                           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();

         

               }

  • 相关阅读:
    IP通信02
    h5网页 微信分享给好友,朋友圈-tp5
    微博常用链接
    Sublime Text3之安裝Emmet及使用技巧
    JS 写入到文件
    PHP之httpRequest
    图片上传预览
    滚动数字时钟
    旋转
    创建JavaScript标准对象--面试经常遇到的问题
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5879261.html
Copyright © 2011-2022 走看看