Pascal's Triangle)

Source: Internet
Author: User

// The following code is compiled on vc2005

//

# Include "stdafx. H"

/*-----------------------------------------------
The following numerical mode is called Pascal's triangle)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
The number on the triangle boundary is 1, and each number inside is the sum of the two numbers located above it.
Write a process and use recursive algorithms to calculate the Pascal triangle.

// In China, which can be called the "Yang Hui triangle or Jia Xian triangle"
-------------------------------------------------*/
Static long getelement (const long row, const long col)
{
// The two peripheral elements of each row are 1
If (1 = col) | (ROW = col ))
Return 1;
Else
// The rest is the sum of the (Col-1) and (COL) elements of the previous row.
Return getelement (Row-1, col-1) + getelement (Row-1, col );
}

Static long pascaltriangle (const long N)
{
Int row;
Int Col;

For (ROW = 1; row <= N; ++ row)
{
For (COL = 1; Col <= row; ++ col)
Printf ("% 4ld", getelement (row, col ));
Printf ("/N ");
}

Return 0;
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Pascaltriangle (5 );
Return 0;
}

 

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.