Prints the first 10 lines of the Yang Hui Triangle.

Source: Internet
Author: User

Prints the first 10 lines of the Yang Hui Triangle. Yang Hui triangles such as:          1                       1         1   1                    1  1      1   2   1                 1  2  1    1    3   3   1              1  3  3  1 1   4   6   4   1            1  4  6  4  1     [Figure 5-1]                   [Figure 5-2]     "Problem analysis" observation figure 5-1, it is not easy to find the law, but if it is converted to figure 5-2, it is not difficult to find that the Yang Hui triangle is actually a small triangular part of a two-dimensional table, assuming that through a two-dimensional array yh storage, each row and the end of the element is 1, and the value of any non-first element yh[i][j] is actually the sum of yh[i-1][j-1] and Yh[i-1][j], and the number of elements in each row is exactly equal to the number of rows. With the value of the array element, to print the Yang Hui Triangle, just control the output starting position.   

#include <iostream>
#include <iomanip>
using namespace Std;
int a[11][11];
int main ()
{
int i,j;
A[1][1]=1;
for (int i=2;i<=10;++i)
{
A[i][1]=1;a[i][i]=1;
for (int j=2;j<=i-1;++j)
A[I][J]=A[I-1][J-1]+A[I-1][J];
}
for (int i=1;i<=10;i++)
{
if (i!=10) COUT<<SETW (30-3*i) << "";
for (int j=1;j<=i;j++)
COUT<<SETW (6) <<a[i][j];
cout<<endl;
}
return 0;
}

Prints the first 10 lines of the Yang Hui Triangle.

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.