// The following code is compiled on VC2005
//
#include "stdafx.h"
/*-----------------------------------------------
下面数值模式称为帕斯卡三角形(Pascal's triangle)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
三角形边界上的数都是1,内部的每个数数是位于它上面的两个数之和。
写一个过程,采用递归算法计算过程计算出帕斯卡三角形。
// in china, 可叫做"杨辉三角或贾宪三角"
-------------------------------------------------*/
static long GetElement(const long row, const long col)
{
// 每行的外围两个元素为1
if ((1 == col) || (row == col))
return 1;
else
// 其余的部分为上一行的(col - 1)和(col)元素之和
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;
}