3032-Yang Hui triangle, 3032-Yang Hui
Problem Description Do you still remember the Yang Hui triangle I learned in middle school? The specific definition is not described here. You can refer to the following figure:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1 Input data contains multiple test instances. The Input of each test instance only contains a positive integer n (1 <= n <= 30 ), number of layers in the Yang Hui triangle to be output. Output corresponds to each input. Please Output the Yang Hui triangle of the corresponding layers. Integers of each layer are separated by a space. An empty line is added after each Yang Hui triangle. Use a 30 × 30 array to save the Yang Hui triangle. Because the input and output are multiple times, the array is a global variable. Input n each time to maintain the array, if n> the number of layers of the current array, the content of the extended array reaches n layers. For example, if n is less than n, It is output directly.
1 #include<iostream> 2 using namespace std; 3 4 int s[30][30] = { 0 }, max=1; 5 void setup(int n){ 6 if(n>max){ 7 for(int i=max;i<n;i++) 8 for(int j=1;j<=i+1;j++) 9 s[i][j]=s[i-1][j-1]+s[i-1][j];10 max=n;11 return;12 }else13 return; 14 }15 void output(int n){16 for(int i=0;i<n;i++){17 cout<<s[i][1];18 for(int j=2;j<=i+1;j++)19 cout<<" "<<s[i][j];20 cout<<endl;21 }22 }23 int main(){24 s[0][1]=1;25 int n;26 while(cin>>n&&n){27 setup(n);28 output(n);29 cout << endl;30 }31 return 0;32 }