码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2249 暴力求组合数

时间:2019-08-24 15:08:51      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:ide   lang   color   and   for   gif   scanf   code   end   

Binomial Showdown
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22692   Accepted: 6925

Description

In how many ways can you choose k elements out of n elements, not taking order into account?
Write a program to compute this number.

Input

The input will contain one or more test cases.
Each test case consists of one line containing two integers n (n>=1) and k (0<=k<=n).
Input is terminated by two zeroes for n and k.

Output

For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 231.
Warning: Don‘t underestimate the problem. The result will fit into an integer - but if all intermediate results arising during the computation will also fit into an integer depends on your algorithm. The test cases will go to the limit.

Sample Input

4 2
10 5
49 6
0 0

Sample Output

6
252
13983816

题意:输入n,k求C(n,k)。

挺无聊的一题,因为答案在2^32内,直接开long long 然后暴力就行了。

AC code:

技术图片
#include<cstdio>
using namespace std;
typedef long long int64;
int64 C(int64 n,int64 k)
{
    int64 a=1,b=1;
    for(int i=1;i<=k;i++)
    {
        a*=(n+1-i);
        b*=i;
        if(!(a%b)){
            a/=b;
            b=1;
        }
    }
    return a/b;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int64 n,k;
    while(~scanf("%lld%lld",&n,&k)&&n+k)
    {
        if(k>n/2)    k=n-k;
        printf("%lld\n",C(n,k));
    }
}
View Code

 

POJ 2249 暴力求组合数

标签:ide   lang   color   and   for   gif   scanf   code   end   

原文地址:https://www.cnblogs.com/cautx/p/11404384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!