杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
下面给出了杨辉三角形的前4行:
1
1 1
1 2 1
1 3 3 1
给出n,输出它的前n行。
输入包含一个数n。
#include <iostream> #include <cstdio> using namespace std; int main() { int n; int s[50][50]; scanf("%d",&n); for(int i = 0;i < n;i ++) { for(int j = 0;j <= i;j ++) { if(j == 0) { s[i][j] = 1; } else if(j == i) { s[i][j] = 1; putchar(‘ ‘); } else { s[i][j] = s[i - 1][j - 1] + s[i - 1][j]; putchar(‘ ‘); } printf("%d",s[i][j]); } putchar(‘\n‘); } }
原文:https://www.cnblogs.com/8023spz/p/10196198.html