Recursively print Yang Hui triangle

Source: Internet
Author: User

/* 05-06-10 21:40
Recursively print the Yang Hui triangle: here you need to know a conclusion:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

I (> = 0) indicates the row number, and J (> = 0) indicates the number of locations as follows:
The first number of rows in 4th is 3: 3! /(2! * (3-2 )!) = 3: The number starts from 0, and the industry starts from 0.
The number of 4th rows is 3: 3! /(0! * (3-0 )!) = 1
Yang Hui triangle meets a combination formula: n! /(! M *! (N-m) using this formula, we can find each number.
The formula is as follows:
Yang Hui triangle number at a certain position = factorial of the current number of rows (starting from 0)/(factorial composed of the number of column positions starts from 0) * (number of rows-factorial of the number of columns ))
*/
# Include <iostream>
Using namespace STD;

Int fun (int n) // n indicates the number of rows
{
If (n = 0)
{
Return 1;
}
Else
{
Return N * Fun (n-1 );
}
}
Int main ()
{
Int N;
Int I, J;
Int C; // Save the temporary variable of the Yang Hui triangle

Int curline; // The factorial of the current row, starting from 0.
Int backline; // number of rows-factorial composed of column locations
Int position; // The factorial of the nth position of the row

Cout <"Enter the number of lines in the Yang Hui triangle :";
Cin> N;

For (I = 0; I <= N; I ++)
{
For (int K = I; k <n; k ++) cout <"; // output the leading space (2), which should be well matched with the trailing space
For (j = 0; j <= I; j ++) // equal sign
{
Curline = fun (I); // factorial of the current number of rows, starting from 0
Backline = fun (I-j); // number of rows-factorial composed of column locations
Position = fun (j); // factorial of the nth position of the row

C = curline/(backline * position); // The value of the Yang Hui triangle.
Cout <C <""; // five spaces
}

Cout <Endl; // output line feed processing
}

Cout <Endl;

System ("pause ");
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.