SDNU 1467.杨辉三角形(水题)

mac2022-06-30  25

Description

杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。 它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。 下面给出了杨辉三角形的前4行: 1 1  1 1  2  1 1  3  3  1 给出n,输出它的前n行。 

Input

输入格式 输入包含一个数n。

Output

输出杨辉三角形的前n行。每一行从这一行的第一个数开始依次输出,中间使用一个空格分隔。请不要在前面输出多余的空格。 

Sample Input

4

Sample Output

1 1  1 1  2  1 1  3  3  1

Hint

1  < =  n  < =  34。 #include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <map> using namespace std; #define ll long long const int maxn = 5100; int n, pascal[100+8][100+8]; int main() { scanf("%d", &n); memset(pascal, 0, sizeof(pascal)); for(int i = 0; i<n; i++)///for(int i = 0; i<10000+8; i++) { for(int j = 0; j <= i; j++) { if(j == 0)pascal[i][0] = 1; else { pascal[i][j] = pascal[i-1][j-1]+pascal[i-1][j]; } } } for(int i = 0; i<n; i++) { bool flag = 0; for(int j = 0; j <= i; j++) { if(flag)printf(" "); flag = 1; printf("%d", pascal[i][j]); } printf("\n"); } return 0; }

 

转载于:https://www.cnblogs.com/RootVount/p/11250020.html

最新回复(0)