How to print Yang Hui triangles in C + +

Source: Internet
Author: User

Here is a part of the Yang Hui Triangle, which we observe to see what the law is:

1

1 1

1 2 1

1 3 3 1

1 4 6) 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

................

It is not difficult to find that the triangle is 1 on both sides, and the value of each number except the boundary is its two-shoulder number and the Yang Hui Triangle is closely related to the two-term theorem, when (a+b) n

The values in N are 0,1,2,3,4 ... when the coefficients correspond to the first 1,2,3,4,5 of the Yang Hui Triangle. Line, the two-item formula is:

This leads to a very important rule: in the Yang Hui Triangle, the above formula in the M is n-1, so that N is k-1, the value is the value of the nth row K in the Yang Hui Triangle, that is

Ck-1n-1 = (n-1)!/((k-1)!* (n-k)!). To output the aesthetics, we want to print a space on the left side of each line, assuming we want to print the first num line of the Yang Hui Triangle, if the last line,

That is, the longest line does not print before the space, the number of spaces before the nth line is num-n.

With this knowledge you can write the code, the complete code is as follows:

1#include <iostream>2 3 //function to find factorial4 intFactorial (intnum)5 {6     if(num = =0)7         return 1;8     Else9     {Ten         intresult =1; One          for(inti =1; I <= num; ++i) AResult *=i; -         returnresult; -     } the } -  - //print functions for Yang Hui triangles - voidfunctionintrowNum) + { -     intvalue; +      for(intn =1; n <= rowNum; ++n)//for each row A     { at          for(inti =0; I <= rownum-n; ++i)//Print the space before each line -Std::cout <<" "; -          for(intK =1; K <= N; ++K)//print each number of lines -         { -Value = Factorial (N-1)/(factorial (K-1) *factorial (N-k)); -Std::cout << Value <<" "; in         } -Std::cout <<Std::endl; to     } + } -  the intMain () * { $     intLineNum;Panax NotoginsengStd::cout <<"Please enter the number of lines to print:"<<Std::endl; -Std::cin >>LineNum; the function (linenum); +System"Pause"); A     return 0; the}

Note: The above code can only print the first 13 rows of the Yang Hui Triangle, because of the function of factorial, we declare and return in the function of factorial the variable type is int type,

In a 32-bit machine, the valid range of the int variable representation number is -2^32~2^32-1, and the factorial of 13 exceeds the representation range of Int. So if you need to print out more lines

Yang Hui Triangle, you can declare a variable of type long or long long.

How to print Yang Hui triangles in C + +

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.