c - 寻找 nCr 值

标签 c combinations

我正在尝试查找 nCr 值。没有错误,但我得到 1 作为所有输入的答案。请帮我找到解决方案。

#include <stdio.h>

int fact(int num)
{
    int f=1,i;
    for(i=1;i<=num;i++)
    {
        f=f*1;
    }
    return f;
}

int main(void) 
{
    int n,r,ncr=0;
    printf("\n enter n and r values");
    scanf("%d%d",&n,&r);
    ncr=(fact(n) / (fact(r) * fact(n-r)));
    printf("\n ncr for %d and %d is %d",n,r,ncr);
    return 0;
}

最佳答案

不应该是f = f *1,而是f = f * i

关于c - 寻找 nCr 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152318/

相关文章:

C-高效代码与好代码

c - Linux 上的一个简单的 C 程序

c - 调用 printf() 会产生什么值?

python - 在特定条件下生成边的组合

php - 如何从 php 中的字符串创建可能的字符串组合?

c - printf 序列点

c - 循环内的循环不运行

java - Java中数字的所有二进制组合的列表

haskell - mapM 如何与 Haskell 中的 const 函数一起工作?

c# - 生成所有组合