-6

なぜ = n なのですか! / ((nk)!*k!) 印刷しない?

また、このコードは以下の問題を解決しますか?

立ち往生。

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

もう少し明確にする:「たとえば、一度に3つ取られた4つのアイテムa、b、c、dの組み合わせは、abc、abd、acd、およびbcdです。つまり、4つの異なる組み合わせが合計4つあります。 「一度に3つ取られた」もの。

#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;
}   
4

2 に答える 2

2

この行:

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

...有効な C コードではありません。!C では「ない」を意味します。

以下を呼び出すことができるように、階乗関数を提供する必要があります。

  result = factorial(n) / (factorial(n-k) * factorial(k));
于 2013-02-09T22:53:01.820 に答える
0

!は 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$
于 2013-02-09T22:53:15.750 に答える