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;
}