zoukankan      html  css  js  c++  java
  • 17.从键盘上输入一个正整数n,请按照以下五行杨辉三角形的显示方式, 输出杨辉三角形的前n行。请采用循环控制语句来实现。 (三角形腰上的数为1,其他位置的数为其上一行相邻两个数之和。) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

    17.从键盘上输入一个正整数n,请按照以下五行杨辉三角形的显示方式,
    输出杨辉三角形的前n行。请采用循环控制语句来实现。
    (三角形腰上的数为1,其他位置的数为其上一行相邻两个数之和。)
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1

    package com.bao;

    import java.util.Scanner;

    public class Yanghui {

    public static void main(String[] args) {
    	Scanner sc=new Scanner(System.in);
    	System.out.print("请输入一个正整数:");
    	int a=sc.nextInt();
    	int[][]b=new int[a][a];
    	for(int i=0;i<a;i++)
    	{
    		for(int j=0;j<=i;j++)
    		{
    			if(j==0||j==a-1)
    			{
    				b[i][j]=1;
    			}
    			else
    			{
    				b[i][j]=b[i-1][j-1]+b[i-1][j];
    			}
    			System.out.print(b[i][j]+" ");
    		}
    		System.out.println("");
    	}
    
    }
    

    }

  • 相关阅读:
    POJ2559/HDU1506 Largest Rectangle in a Histogram (cartesian tree)
    POJ2201 Cartesian Tree (cartesian tree)
    一本通1007
    一本通1006
    一本通1005
    一本通1004
    一本通1003
    一本通1002
    一本通1001
    一本通1000
  • 原文地址:https://www.cnblogs.com/nicebaby/p/5877935.html
Copyright © 2011-2022 走看看