C + + print Yang Hui's triangle __c++

Source: Internet
Author: User

Print out the following Yang Hui's triangles (10 lines are required to print)

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 1

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


Required to print out 10 lines


Yang Hui's triangle: The end point is 1, each number equals the sum of two digits on its shoulder.


#include "stdafx.h"
#include <iostream>
using namespace Std;


int _tmain (int argc, _tchar* argv[])
{
int array[10][10]; Defines a 10*10 array of two dimensions
int i,j;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
array[i][j]=0; Assigns an initial value so that each element of the array is 0
}
}


for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
if (j==0)
{
Array[i][j]=1; Make each number 1 on the first column
}
}
}


for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
if (j>0&&i>0&&j<=i)//Here J is required to be less than or equal to I so that you can only perform the required operation in the triangle
{
ARRAY[I][J]=ARRAY[I-1][J-1]+ARRAY[I-1][J]; The characteristic of a Yang Hui's triangle is equal to the sum of two numbers on its shoulders, i.e. array[i][j]=array[i-1][j-1]+array[i-1][j];
}
}
}


for (i=0;i<10;i++)
{
for (j=0;j<=i;j++)//output only triangles, not entire matrices
{
cout<<array[i][j]<< "";
}
cout<<endl; Once you have finished outputting a line, you can wrap it.
}
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.