// 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;
}