zoukankan      html  css  js  c++  java
  • 用java输出杨辉三角

    杨辉三角:它的两个边都是1,内部其它都是肩上两个数的和

    第一种:

    package aaa;
    
    public class YangHui {
    
    	public static void main(String[] args) {
    		/**
    		 * 6行6列的杨辉三角
    		 */
    		int row = 6;//行数
    		int[][] yanghui = new int[row][row];//6行6列数组
    		
    		for (int i = 0; i < row; i++){//行
    			for(int j = 0;j<= i;j++){//列
    				if (j==0 || j==i){
    					yanghui[i][j]=1;
    					
    				}else{
    					yanghui[i][j]=yanghui[i-1][j-1]+yanghui[i-1][j];
    				}
    				System.out.print(yanghui[i][j]+" ");
    			}
    			System.out.println();
    		}	
    	}
    }
    

    第二种:等腰三角形

    package aaa;
    
    public class YangHui {
    
    	public static void main(String[] args) {
    		/**
    		 * 8行8列的杨辉三角
    		 */
    		int row = 6;//行数
    		int[][] yanghui = new int[row][row];//6行6列数组
    		
    		for (int i = 0; i < row; i++){//行
    			for(int j = 0;j<= i;j++){//列
    				if (j==0 || j==i){
    					yanghui[i][j]=1;
    					
    				}else{
    					yanghui[i][j]=yanghui[i-1][j-1]+yanghui[i-1][j];
    				}
    //				System.out.print(yanghui[i][j]+" ");
    			}
    //			System.out.println();
    		}
    //等腰输出处理
    		for (int i = 0; i < row; i++){
    			int num = row -i;
    			for(int j = 0;j<= num;j++){
    				System.out.print(" ");	
    		}
    			for(int k= 0;k<= i;k++){
    				System.out.print(yanghui[i][k]+" ");	
    		}
    			System.out.println();
    	}
    	
    	}
    }
    

      

  • 相关阅读:
    Codeforces 813F Bipartite Checking 线段树 + 并查集
    Codeforces 263E Rhombus (看题解)
    Codeforces 173E Camping Groups hash
    Codeforces 311C Fetch the Treasure 取模意义下的最短路 (看题解)
    R 培训之 Table
    Docker命令详解
    Celery的实践指南
    Using Celery with Djang
    PostgreSQL
    改时区参考
  • 原文地址:https://www.cnblogs.com/wangcp-2014/p/11370993.html
Copyright © 2011-2022 走看看