實驗7-二維陣列的運用 楊輝三角
阿新 • • 發佈:2019-01-26
楊輝三角
Time Limit: 1000MS Memory Limit: 65536KBProblem Description
11 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
上面的圖形熟悉嗎?它就是我們中學時候學過的楊輝三角。
Input
輸入資料包含多組測試資料。每組測試資料的輸入只有一個正整數n(1≤n≤30),表示將要輸出的楊輝三角的層數。
輸入以0結束。
Output
對應於每一個輸入,請輸出相應層數的楊輝三角,每一層的整數之間用一個空格隔開,每一個楊輝三角後面加一個空行。Example Input
2 3 0
Example Output
1 1 1 1 1 1 1 2 1
Hint
Author
ZJGSU#include <stdio.h> #include <stdlib.h> int main() { int a[35][35] = {}, i, j, n; while(scanf("%d", &n) && n!= 0) { for(i = 0; i < n; i++) { a[i][0] = 1; } for(i = 1; i < n ;i++) { for(j = 1; j < n; j++) { a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; } } for(i = 0; i <n;i++) { for(j = 0; j < n; j++) { if(a[i][j] == 0) { printf("\n"); break; } else { if(j == 0) { printf("%d", a[i][j]); } else { printf(" %d", a[i][j]); } } } } printf("\n\n"); } return 0; }