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("");
	}

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM