ORIGIN

HDU-2032 Yang Hui triangle

ACM 1 mins261 words

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:

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]
$$

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|