`
txf2004
  • 浏览: 6867314 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

《第二周实验报告2-1》---用自定义求阶乘

 
阅读更多
/*(2‐1)组合数求解公式为
 n      m!
Cm = --------
     n!(m-n)!
编程序输入m,n,输出组合数,要求用自定义
函数实现求阶乘。
* 算法说明:
*			1.定义阶乘函数时,首先定义一个变量,用来储存数据并赋初值为1;
*			2.对一些有规律的数进行运算,用循环完成
*			3.对数据运算的简单处理
*/
#include <iostream>
using namespace std;
long fac(int n) //求n 的阶乘
{
	int st_n = 1;

	while (n > 0)
	{
		st_n = n*st_n;
		--n;
	}

	return st_n;
}
int main()
{
	int m, n, C_mn;

	cout <<"请输入要求组合数m,n的值:"<< endl;
	cin >> m >> n;

	C_mn = fac(m)/(fac(n) * fac(m-n));

	cout << C_mn << endl;

	system("PAUSE");
	return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics