2013-02-09 221 views
-6

原因是= n! /((n-k)!* k!)不打印?组合公式“n!/((n-k)!* k!)”不起作用

此代码还可以解决以下问题吗?

卡住了。

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

多一点澄清:“。例如,的四个项目A,B,C中的组合,D截取3在时间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; 
} 
+0

你知道什么!在C吗? – 2013-02-09 22:51:25

+5

'!'不符合你的想法。制作你自己的阶乘功能。 – Blender 2013-02-09 22:51:29

+3

为什么你在尝试做东西之前不学习语言? – 2013-02-09 22:53:39

回答

2

这条线:

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

...是不是有效的C代码。 C中的!表示“不”。

您需要提供a factorial function,使您可以拨打:

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

!不是NOT运营商C.使用此阶乘函数。

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)); 

没有任何意义,我,因为这两个nfrkfr是零,但我想你想获得的代码进行编译,完成逻辑之前。

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

#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$ 
+0

当我添加结果= factorial(n)/(factorial(n-k)* factorial(k))时,出现'使用'factorial'超范围声明'的错误。取代我的结果。 – user1787331 2013-02-09 23:09:47

+1

@ user1787331更新了答案。也许你把函数放在错误的地方? – user000001 2013-02-09 23:13:31

+0

@ user1787331:你真的需要学习[C语言编程](http://www.amazon.co.uk/exec/obidos/ASIN/0131103628/johnsydotorg-21)才能从这里进步。 StackOverflow不能教你编程。 – Johnsyweb 2013-02-09 23:34:55