codeup之杨辉三角
生活随笔
收集整理的這篇文章主要介紹了
codeup之杨辉三角
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
按要求輸入如下格式的楊輝三角
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
最多輸出10層
Input
輸入只包含一個正整數n,表示將要輸出的楊輝三角的層數。
Output
對應于該輸入,請輸出相應層數的楊輝三角,每一層的整數之間用一個空格隔開
Sample Input Copy
5
Sample Output Copy
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
solution
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
if(n > 10 || n < 1){
return -1;
}
int a[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){
if(j == 0 || j == i)
a[i][j] = 1;
else{
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
總結
以上是生活随笔為你收集整理的codeup之杨辉三角的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dll正由另一进程使用,因此该进程无法访
- 下一篇: Canon LBP2900安装Linux