用二维数组完成杨辉三角的练习

  • Post author:
  • Post category:其他


用二维数组完成杨辉三角的练习

1

1    1

1    2    1

1    3    3    1

1    4    6    4    1

…..

 1 package shulie;
 2 
 3 import java.util.Scanner;  4 /**  5  *  6  * @author likainian  7  * 1784186573@qq.com  8 */  9 public class Yanghui{ 10 public static void main(String[]agrs){ 11 System.out.println("请输入行数:"); 12 Scanner s=new Scanner(System.in); 13 int m=s.nextInt(); 14 15 int[][] yanghui=new int[m][]; 16 17 for(int i=0;i<yanghui.length;i++){ 18 yanghui[i]=new int[i+1]; 19  } 20 for(int i=0;i<yanghui.length;i++){ 21 for(int j=0;j<yanghui[i].length;j++){ 22 yanghui[i][0]=yanghui[i][j]=1; 23 if(i>1&&j>0&&j<i){ 24 yanghui[i][j]=yanghui[i-1][j]+yanghui[i-1][j-1]; 25  } 26  } 27  } 28 for(int i=0;i<yanghui.length;i++){ 29 for(int j=0;j<yanghui[i].length;j++){ 30 System.out.print(yanghui[i][j]+"\t"); 31  } 32  System.out.println(); 33  } 34  s.close(); 35  } 36 }

转载于:https://www.cnblogs.com/String-likainian/p/5824530.html