3032-Yang Hui triangle, 3032-Yang Hui

Source: Internet
Author: User

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 } 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.