110gogo

Java练习1:

杨辉三角,是二项式系数在三角形中的一种几何排列,在中国南宋数学家杨辉1261年所著的《详解九章算法》一书中出现。

结构如下:

代码如下:

 1 package Jacklu;
 2 
 3 import java.util.Scanner;
 4 
 5 public class YanghuiTriangle {
 6     public static void main(String[] args) {
10         System.out.println("输出几行?");
11         Scanner sc = new Scanner(System.in);
12         int a = sc.nextInt();
13         int[][] arr = new int[a][a];
14         for (int i = 0; i < arr.length; i++) {
15             for (int j = 0; j <= i; j++) {
16                 arr[i][0] = 1;
17                 arr[i][i] = 1;
18                 if (i <= 1 || j == 0) {
19                     System.out.print(arr[i][j]+" ");
20                 }else{
21                     arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
22                     System.out.print(arr[i][j]+" ");
23                 }
24             }
25             System.out.println();
26         }
27     }
29 }

分类:

技术点:

相关文章:

  • 2021-12-29
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
猜你喜欢
  • 2022-12-23
  • 2021-12-09
  • 2021-07-26
  • 2021-08-29
  • 2021-09-22
相关资源
相似解决方案