題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2032
Problem Description
還記得中學時候學過的楊輝三角嗎?具體的定義這裡不再描述,你可以參考以下的圖形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Input
輸入資料包含多個測試執行個體,每個測試執行個體的輸入隻包含一個正整數n(1<=n<=30),表示将要輸出的楊輝三角的層數。
Output
對應于每一個輸入,請輸出相應層數的楊輝三角,每一層的整數之間用一個空格隔開,每一個楊輝三角後面加一個空行。
Sample Input
2 3
Sample Output
1
1 1
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int main() {
int n;
int a[35][35];
mem(a,0);
a[1][1]=1;
a[2][1]=a[2][2]=1;
for(int i=3; i<35; i++) {
for(int j=1; j<35; j++) {
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
while(scanf("%d",&n)!=EOF) {
for(int i=1; i<=n; i++) {
for(int j=1; j<=i; j++) {
if(j==1) {
printf("%d",a[i][j]);
} else {
printf(" %d",a[i][j]);
}
}
printf("\n");
}
printf("\n");
}
return 0;
}