用C二项式系数 [英] Binomial coefficient in C

查看:135
本文介绍了用C二项式系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,你可以找到这个问题我想解决:

有关整数n和k(0℃= K< = N< 1001)。确定(二项式系数)

For integers n and k (0<=k<=n<1001) determine (binomial coefficient).

输入

标准输入的第一行包含一个整数T(T&LT; 1001)。这是测试用例的数量

The first line of the standard input contains one integer t (t<1001) which is the number of test cases.

在各下一牛逼行有数目n和k

In each of the next t lines there are numbers n and k.

输出

对于每个测试打印(二项式系数)。

For each test print (binomial coefficient).

例如:

Input
3
0 0
7 3
1000 2
Output:
1
35
499500

我似乎无法找到我的解决方案什么不对(除了它是写非常差 - 我已经开始编程相当最近):

I can't seem to find anything wrong in my solution (other than it's written very poorly - I've started programming quite recently):

#include <stdio.h>

int main()
{
    unsigned long int t,n,k,binomial=1;
    unsigned long int number=1;

    for(scanf("%lu",&t);t>0;t--)
    {
        scanf("%lu%lu",&n,&k);
        if(k<(n/2)) k=n-k;
        for(binomial=1,number=1;n>k;k++)
        {
            binomial=binomial*(k+1)/number;
            number++;
        }
        printf("%lu\n",binomial);
    }

    return 0;
}

它工作得很好的例子输入,但解决的办法是通过一个问题来判断网站

It works fine for the example input, but the solution is judged via a problem site

(http://www.spoj.pl/SHORTEN/problems/BINOMIAL/english/)

(http://www.spoj.pl/SHORTEN/problems/BINOMIAL/english/)

和溶液不被接受。我尝试过其他的投入也和他们都还给右声道输出。我的问题是:是否有一个原因,这个解决方案是无效的。

and the solution is not accepted. I tried other inputs too and all of them gave back the right output. My question is: Is there a reason why this solution is invalid?

推荐答案

由于1000C500大约是300位,它不能被储存在一个unsigned long。总之,你需要重新开始,并认为更好的技术。

As 1000C500 is around 300 digits, it cant be stored in an unsigned long. In short, you need to start over and think of a better technique.

这篇关于用C二项式系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆