求组合数(函数)

185 阅读1分钟

题目描述

编写一个函数,m和n是参数,按以下公式求组合数的值,假设m,n都是正整数,且m>=n。

 

主函数负责输入m和n的值,并调用函数求出组合数的值,并输出

输入

测试数据的组数 t

第一组m,n

第二组m,n

..........

输出

第一组组合数的值

第二组组合数的值

..........

输入样例1

3
8 5
6 3
10 8

输出样例1

56
20
45

代码

#include<iostream>
using namespace std;
long long fact(int n)
{
	int i=1,fact=1;
	for(i=1;i<=n;i++)
	fact=fact*i;
	return fact;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int m,n;
		cin>>m>>n;
		cout<<fact(m)/(fact(n)*fact(m-n))<<endl;
	}
}