杭電oj 網站實時狀態 (hdu.edu.cn)
2032 楊輝三角
楊輝三角,是二項式系數在三角形中的一種幾何排列,中國南宋數學家楊輝1261年所著的《詳解九章演算法》一書中出現,在歐洲,帕斯卡(1623----1662)在1654年發現這一規律,所以這個表又叫做帕斯卡三角形,帕斯卡的發現比楊輝要遲393年,比賈憲遲600年,
問題描述
還記得中學時候學過的楊輝三角嗎?具體的定義這里不再描述,你可以參考以下的圖形:
1
1 1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
輸入
輸入資料包含多個測驗實體,每個測驗實體的輸入只包含一個正整數n(1<=n<=30),表示將要輸出的楊輝三角的層數,
輸出
對應于每一個輸入,請輸出相應層數的楊輝三角,每一層的整數之間用一個空格隔開,每一個楊輝三角后面加一個空行,
示例輸入
2 3
示例輸出
1
1 1
1
1 1
1 2 1
package Hdoj;
import java.util.Scanner;
public class Yanghuisanjiao {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int arr[][] = new int[n][];
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[i + 1];
for (int j = 0; j < arr[i].length; j++) {
if (j == 0 || arr[i].length - 1 == j) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
System.out.print(arr[i][j] + (i == j ? "" : " "));
}
System.out.println();
}
System.out.println();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499320.html
標籤:其他
上一篇:Python基礎day23:絕對匯入和相對匯入、模塊化編程簡介、軟體開發目錄規范、常見內置模塊collections和time
