杨辉三角

网友投稿 265 2022-09-22

杨辉三角

package test.forr;import java.util.Arrays;import java.util.Scanner;public class YangHuiSanJiao {    /*   * 1    * 1 1    * 1 2 1    * 1 3 3 1    * 1 4 6 4 1   * 1 5 1010 5 1   */  public static void main(String[] args) {    Scanner s = new Scanner(System.in);    System.out.println("请输入一个杨辉三角的基数");    int[][] a = new int[s.nextInt()][];    //嵌套循环赋值+遍历    for (int i = 0; i < a.length; i++) {      a[i] = new int[i + 1];      for (int j = 0; j < a[i].length; j++) {        if (i == 0 || j == 0 || j == i)          a[i][j]=1;        else//第二行开始,上面的两个数相加==下面的数(1+1=2,4=1+3,6=3+3)注意观察规律          a[i][j] = a[i - 1][j] + a[i - 1][j - 1];        // 输出数组元素        System.out.print(a[i][j] + "\t");      }//循环完一次换行      System.out.println();    }  }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CSS作业
下一篇:丁道师:我的一位企业家朋友,纠结“选苹果还是华为”!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~