求组合

#include <iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long int64;
int64 work(int64 n,int64 k)
{
    if (k>n/2)k=n-k;
    int64 a=1,b=1;
    for (int i=1;i<=k;i++)
    {
        a*=n+1-i;
        b*=i;
        if (a%b==0)a/=b,b=1;
    }
    return a/b;
}
int main()
{
    int n,k;
    while (~scanf("%d%d",&n,&k)&&n)
    {
        printf("%ld\n",work(n,k));
    }
    return 0;
}

  

 求组合_第1张图片

 

 

 

 

#include <iostream>
using namespace std;
int work(int n,int k)
{
    return k==0?1:work(n,k-1)*(n-k+1)/k;
}
int main()
{
    int n,k;
    while (scanf("%d%d",&n,&k)==2)
    {
        printf("%d\n",work(n,k));
    }
    return 0;
}

  

 

求组合_第2张图片

你可能感兴趣的:(组合)