鱼C论坛

 找回密码
 立即注册
查看: 3240|回复: 4

阶乘,包括对零的处理,希望大神们帮我简化一下

[复制链接]
发表于 2019-8-30 22:49:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
int jiechen(int num)
{
   int result;
  static int count=1;
  if(count==1&&num!=0)
  {
       count++;
     result=num*jiechen(num-1);

  }
  else if((count==1)&&(num==0))
  {
   result=0;
  }
else
{
      if(num>0)
      {
       result=num*jiechen(num-1);

      }
      else
      {
       result=1;
       count=1;
      }
}
return result;
}
int main()
{
int a,b;
while(1)
{
printf("请输入整数:\n");
scanf("%d",&a);
  b= jiechen(a);
printf("结果为:%d\n",b);
}
return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-31 00:34:13 | 显示全部楼层
本帖最后由 jackz007 于 2019-8-31 10:26 编辑

        这个代码简化的是否到位?

  1. #include <stdio.h>

  2. unsigned int jiechen(int num)
  3. {
  4.         unsigned int result                                               ;
  5.         int k                                                             ;
  6.         if(num > 0) for(result = 1 , k = 1 , k <= num ; k ++) result *= k ;
  7.         else result = 0                                                   ;
  8.         return result                                                     ;
  9. }

  10. main(void)
  11. {
  12.         int a                                                             ;
  13.         unsigned int b                                                    ;
  14.         while(1) {
  15.                 printf("请输入整数:")                                    ;
  16.                 if(scanf("%d" , & a) == 1) {
  17.                         b = jiechen(a)                                    ;
  18.                         printf("结果为:%u\n" , b)                        ;
  19.                 } else {
  20.                         break                                             ;
  21.                 }
  22.         }
  23. }
复制代码


        如果规定必须采用递归算法,那就是下面的代码:

  1. #include <stdio.h>

  2. unsigned int jiechen(int num)
  3. {
  4.         unsigned int result                          ;
  5.         result = 0                                   ;
  6.         if (num > 1) result = num * jiechen(num - 1) ;
  7.         else if(num == 1) result ++                  ;
  8.         return result                                ;
  9. }

  10. main(void)
  11. {
  12.         int a                                        ;
  13.         unsigned int b                               ;
  14.         while(1) {
  15.                 printf("请输入整数:")               ;
  16.                 if(scanf("%d" , & a) == 1) {
  17.                         b = jiechen(a)               ;
  18.                         printf("结果为:%u\n" , b)   ;
  19.                 } else {
  20.                         break                        ;
  21.                 }
  22.         }
  23. }
复制代码


       这个代码采用 32 位整型变量,只能计算 12 以内的阶乘,如果改用 64 位整型变量,则可以计算 20 以内的阶乘,如果超出,计算结果将是溢出后的错误数值:

  1. #include <stdio.h>

  2. unsigned long long jiechen(int num)
  3. {
  4.         unsigned long long  result                    ;
  5.         result = 0                                    ;
  6.         if (num > 1) result = num * jiechen(num - 1)  ;
  7.         else if(num == 1) result ++                   ;
  8.         return result                                 ;
  9. }

  10. main(void)
  11. {
  12.         int a                                         ;
  13.         unsigned long long b                          ;
  14.         while(1) {
  15.                 printf("请输入整数:")                ;
  16.                 if(scanf("%d" , & a) == 1) {
  17.                         b = jiechen(a)                ;
  18.                         printf("结果为:%I64u\n" , b) ; // 这一句有问题,试试写成这样:printf("结果为:%llu\n" , b)   ;
  19.                 } else {
  20.                         break                         ;
  21.                 }
  22.         }
  23. }
复制代码


        上述代码采用 tdm-gcc 成功编译运行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-31 23:09:57 | 显示全部楼层
谢谢大哥,我看看,很OK
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-1 13:55:22 | 显示全部楼层
可以用 <math.h>里的 tgamma()函数,当输入n为整数时,返回 (n - 1)!
  1. #include <math.h>
  2. #include <stdio.h>

  3. long long jiecheng(int n)
  4. {
  5.       if(n >= 0)
  6.             return (long long)tgamma(n + 1);
  7. }

  8. int main()
  9. {
  10.       test = jiecheng(3);
  11.       printf("%lld", test);
  12. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-1 14:14:35 | 显示全部楼层
  1. \Gamma(s)=\int_{0}^{+\infty} x^{s-1} e^{-x} d x
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 03:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表