HDU-2032 Yang Hui triangle

See the article on https://dyingdown.github.io/2019/12/20/HDU-2032%20Yang-Hui-triangle/

Yang Hui triangle

Remember the Yang Hui triangle you learned in middle school? The specific definition is not described here, you can refer to the following figure:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KgzZqIXC-1576812717168)(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSp8ifUZL3-2LFdhmkXlBN0fqY_EPEwvMV3MtqLqFrn1YbcUIJu&s)]

Input

The input data contains multiple test instances. The input of each test instance contains only a positive integer n (1 <= n <= 30), which represents the number of layers of the Yang Hui triangle to be output.

Output

Corresponding to each input, please output the Yanghui triangle of the corresponding number of layers. The integers of each layer are separated by a space, and a blank line is added after each Yanghui triangle.

Analysis

As you can see from the picture, for each unit, there is :
y [ i ] [ j ] = y [ i − 1 ] [ j − 1 ] + y [ i − 1 ] [ j ] y[i][j] = y[i - 1][j - 1] + y[i - 1][j] y[i][j]=y[i1][j1]+y[i1][j]

Code

#include<bits/stdc++.h>

using namespace std;

long long y[50][50];
void Yang() {
	for(int i = 0; i < 50; i ++) {
		y[i][0] = 0;
	}
	y[0][1] = 1;
	for(int i = 1; i <= 30; i ++) {
		for(int j = 1; j <= i; j ++) {
			y[i][j] = y[i - 1][j - 1] + y[i - 1][j];
		}
	}

}
int main() {
	Yang();
	int n;
	while(cin >> n) {
		for(int i = 1; i <= n; i ++) {
			for(int j = 1; j <= i; j ++) {
				if(j == 1) cout << y[i][j];
				else cout << " " << y[i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值