⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 Yanghui Triangle.杨辉三角. 用二项式展开来做的.
💻 CPP
字号:
/***************************************************************\
* Copyright (c) 2009 eryar All rights reserved.                 *
*                                                               *
* File    :  Main.CPP                                           *
* Date    :  2009-03-15  21:12                                  *
* Author  :  eryar@163.com                                      *
*                                                               *
* Description:    Yanghui Triangle. 杨辉三角                    *
*                                                               *
\***************************************************************/

#include <iomanip>
#include <iostream>
using namespace std;

int		Factorial(int base);
int		Permutation(int n, int r);
int		Combination(int n, int r);
void	YanghuiTriangle(int row);

int main(int argc, char* argv[])
{
	int		iRow = 0;

	cout<<"Input the Row of Yanghui Triangle : ";
	cin>>iRow;
	YanghuiTriangle(iRow);

	return 0;
}

/*	Parameter	:	int
 *	Return		:	int
 *	Description	:	calculate the factorial.
 */
int		Factorial(int base)
{
	if (base == 1 || base == 0)
		return 1;
	else
		return base * Factorial(base - 1);
} // End of Factorial

/*	Parameter	:	int , int
 *	Return		:	int
 *	Description	:	calculate the permutation
 */
int		Permutation(int n, int r)
{
	return ( Factorial(n) / Factorial(n - r));
} // End of Permutation

/*	Parameter	:	int , int
 *	Return		:	int
 *	Description	:	calculate the combination.
 */
int		Combination(int n, int r)
{
	return	( Factorial(n) / Factorial(r) / Factorial(n - r));
} // End of Combination

/*	Parameter	:	int	
 *	Return		:	void
 *	Description	:	Show the Yanghui triangle.
 */
void	YanghuiTriangle(int row)
{
	if( row > 12) {
		cout<<"The row must less than 12. "<<endl;
		cout<<"The program will exit!"<<endl;
		exit(0);
	}

	for(int i=0; i<=row; ++i) {
		for(int k=0; k<(row-i); ++k)
			cout<<"  ";
		for(int j=0; j<=i; ++j) {
			cout<<setw(5)<<Combination(i, j);
		}
		cout<<endl;
	}

} //End of YanghuiTriangle

/*----------------------------
Input the Row of Yanghui Triangle : 9
                      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
      1    8   28   56   70   56   28    8    1
    1    9   36   84  126  126   84   36    9    1

----------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -