Java学习~杨辉三角

要点:双层for循环

重点理解

yanghui[i][j]=yanghui[i-1][j]+yanghui[i-1][j-1];

package yanghui;
public class Yanghui{
      public static void main(String[] args){
		int [][] yanghui =new int [10][];
		for(int i=0;i<yanghui.length;i++)
		{
		yanghui[i]=new int[i+1];	
		}
		
		for(int i=0;i<yanghui.length;i++) {
			for(int j=0;j<yanghui[i].length;j++)
			{
				yanghui[i][0]=yanghui[i][i]=1;
				if(i>1&&j<i&&j>0)
				{
					yanghui[i][j]=yanghui[i-1][j]+yanghui[i-1][j-1];
				}
			}
		}
		for(int i=0;i<yanghui.length;i++) {
			for(int j=0;j<yanghui[i].length;j++) {
			System.out.print(yanghui[i][j]+"\t");	
			}
			System.out.println();
		}
      }
     }

 

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/qq_40843903/article/details/81951997
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞