【问题标题】:Combination formula "n! / ((n-k)!*k!)" doesn't work [closed]组合公式“n!/((n-k)!*k!)”不起作用[关闭]
【发布时间】:2013-02-09 22:48:26
【问题描述】:

为什么=n! / ((n-k)!*k!) 不打印?

这段代码也能解决下面的问题吗?

卡住了。

"The number of combinations of n things taken k at a time as an integer"

再澄清一点:“例如,a,b,c,d 四个项目一次取三个的组合是 abc,abd,acd,bcd。也就是说,总共有四种不同的四样东西的组合“一次取三样”。”

#include <stdio.h>
#include <math.h>   

int main (void)
{
    int z = 0, n_in, k_in, k = 0, n = 0, result, nfr = 0, kfr = 0;

    do
    {
        printf("Enter the number of items in the list (n):");
        scanf("%d*c", &n_in);

        if (n_in>1 && n_in<11)
        {

            printf("Enter the number of items to choose (k)");
            scanf("%d*c", &k_in);

            if (k_in>0 && k_in<5)
            {

                if (k_in <= n_in)
                {

                    k_in = k;
                    n_in = n;


                    result = n! / ((n-k)!*k!);


                    z = 1;

                }

                else
                    printf("?Please Try again k must be less than n \n");
            }

            else
                printf("?Invalid input: Number must be between 1 and 4 \n");

        }

        else
            printf("?Invalid input: Number must be between 1 and 10 \n");


    } while (z == 0);

    result = (nfr / (nfr * kfr));
    printf("k value = %d n value = %d the result is %d", nfr, kfr, result);


    return 0;
}   

【问题讨论】:

标签: c function combinations


【解决方案1】:

这一行:

result = n! / ((n-k)!*k!);

...不是有效的 C 代码。 ! 在 C 中的意思是“不”。

您需要提供a factorial function,以便您拨打电话:

  result = factorial(n) / (factorial(n-k) * factorial(k));

【讨论】:

    【解决方案2】:

    ! 不是 C 中的 NOT 运算符。请改用此阶乘函数。

    int factorial(int n)
    {
      return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
    }
    

    所以你的计算是:

    result = factorial(n) / (factorial(n-k)*factorial(k));
    

    可能有更快的方法来做到这一点,但这是可读的。

    还有这一行

    result = (nfr / (nfr * kfr));
    

    对我来说没有任何意义,因为 nfr 和 kfr 都为零,但我猜你想在完成逻辑之前编译代码。

    编辑: 完整的代码应该是这样的:

    #include <stdio.h>
    #include <math.h>
    
    int factorial(int n)
    {
        return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
    }
    
    int main (void)
    {
        int z = 0, n_in, k_in, k = 0, n = 0, result, nfr = 0, kfr = 0;
        do
        {
            printf("Enter the number of items in the list (n):");
            scanf("%d*c", &n_in);
    
            if (n_in>1 && n_in<11)
            {
                printf("Enter the number of items to choose (k)");
                scanf("%d*c", &k_in);
                if (k_in>0 && k_in<5)
                {
                    if (k_in <= n_in)
                    {
                        k_in = k;
                        n_in = n;
                        result = factorial(n) / (factorial(n-k)*factorial(k));
                        //result = n! / ((n-k)!*k!);
                        z = 1;
                    }
                    else
                        printf("?Please Try again k must be less than n \n");
                }
                else
                    printf("?Invalid input: Number must be between 1 and 4 \n");
            }
            else
                printf("?Invalid input: Number must be between 1 and 10 \n");
        } while (z == 0);
        //result = (nfr / (nfr * kfr));
        printf("k value = %d n value = %d the result is %d\n", nfr, kfr, result);
        return 0;
    }
    

    输出:

    ~/so$ gcc test.cc
    ~/so$ ./a.out 
    Enter the number of items in the list (n):3
    Enter the number of items to choose (k)2
    k value = 0 n value = 0 the result is 1
    ~/so$
    

    【讨论】:

    • 当我添加结果 = factorial(n) / (factorial(n-k) * factorial(k));代替我的结果。
    • @user1787331 更新了答案。也许你把函数放在了错误的地方?
    • @user1787331:你真的需要学习the C programming language才能从这里进步。 StackOverflow 不能教你编程。
    • @user000001 非常感谢!这帮助我解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    相关资源
    最近更新 更多