• N的10000的阶乘


    N!

    Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 54   Accepted Submission(s) : 11
    Problem Description
    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
     
    Input
    One N in one line, process to the end of file.
     
    Output
    For each N, output N! in one line.
     
    Sample Input
    1 2 3
     
    Sample Output
    1 2 6
     
    10000的阶乘。。。位数达到了天文级。。。所以不得不用高精度算法,而且为了提高效率,建议用万进制的高精度算法。
     
    AC代码
    #include<cstdio>
    #include<cstring>
    int arr[10000];
    int main()
    {
        int n;
       while(scanf("%d",&n)!=EOF)
       {
        int c=0,s;
        int i,j;
        memset(arr,0,sizeof(arr));
        arr[0]=1;
        for ( i=2;i<=n;i++)
        {
           for (j=0;j<10000;j++)
           {
               s=arr[j]*i+c;
               arr[j]=s%10000;
               c=s/10000;
           }
        }
        int z,q;
        for( z=9999;z>=0;z--)
        {
         if (arr[z]){break;}
        }
        printf("%d",arr[z]);
        for (q=z-1;q>=0;q--){printf("%04d",arr[q]);}
        printf("\n");
       }
     return 0;
    }
    特别注意,,
    用此方法,应当逆序输出,因为数组高位存储的恰恰也是结果的前几位。
    输出的时候printf("%04d",arr[q]);}。。04d使得在4位没满的情况下高位补0,否则0会被吃掉,但又要注意,第一位不同高位补零,所以单独输出。
     
  • 相关阅读:
    数据结构的入门
    Google 插件
    树莓派的第一次
    MySQL下载与安装
    SVN图标不显示问题
    excel 批量生成SQL语句
    版本管理工具
    RSA加密、解密、签名、验签的原理及方法
    获取客户端内网IP
    eclipse 添加svn插件
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2793866.html
Copyright © 2020-2023  润新知