高精度算阶乘

  1. /*
  2. 习题 92:高精度算阶乘★★
  3. 题目描述:
  4. 算出n!的完整结果,其中n!=1*2*3*...*n
  5. 输入:
  6. 多组测试数据,一行一组,每行仅一个数n
  7. 其中0<=n<=10000
  8. 输出:
  9. 输出相应的n!的结果,必须精确到个位
  10. 样例输入:
  11. 10
  12. 20
  13. 100
  14. 样例输出:
  15. 3628800
  16. 2432902008176640000
  17. 933262154439441526816992388562667004907159682643816214685929
  18. 638952175999932299156089414639761565182862536979208272237582
  19. 51185210916864000000000000000000000000
  20. 其它信息:
  21. 最后一个100!的结果由于过长,故拆分成三行,每行60字符,请见谅
  22. 难度:Easy
  23. */
  24. #define MAX 10000
  25. #include<stdio.h>
  26. #include<string.h>
  27. int main()
  28. {
  29.     int n;
  30.     int i,j,k;
  31.     char result[MAX];
  32.     char tmp[MAX];
  33.     while(scanf("%d",&n)!=EOF){
  34.         if(n==0){
  35.             printf("1/n");
  36.             continue;
  37.         }
  38.         memset(result,'0',MAX);
  39.         memset(tmp,'0',MAX);
  40.         tmp[0]='1';
  41.         for(i=1;i<=n;i++){
  42.             memset(result,'0',MAX);
  43.             for(j=0;j<i;j++){
  44.                 for(k=0;k<MAX;k++){
  45.                     result[k]=result[k]+(tmp[k]-48);
  46.                     if(result[k]>57){
  47.                         result[k+1]=result[k+1]+(result[k]-48)/10;
  48.                         result[k]=result[k]-(result[k]-48)/10*10;
  49.                     }
  50.                 }
  51.             }
  52.             memcpy(tmp,result,MAX);
  53.         }
  54.         for(i=MAX-1;i>=0;i--)
  55.             if(result[i]!='0')break;
  56.         for(;i>=0;i--)printf("%c",result[i]);
  57.         printf("/n");
  58.     }
  59. }
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值