落絮飞雁

顺流而下,把梦做完

HDOJ1042:N!–大数

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以内的阶乘,典型的大数问题.这里用的是 五位一存的方法. 关于X位一存的解释可以看这篇文章. 注意特殊情况(0,1)下的处理.

#include 
#include 
#define MOD 100000
int main()
{
    int a[8000];    
    int i,k,n,t,la;
    while(~scanf("%d",&n))
    {
        if(n==0||n==1) {printf("1\n");continue;}

        a[0] = 1; 
        la = 1;
        t = 0;
        for(k=2; k 0)   
            {    
                a[la++] = t;
                t = 0; 
            } 
        }
        printf("%d",a[la-1]);
        for(i=la-2; i>=0; i--)
            printf("%05d",a[i]);
        printf("\n");
    }
    return 0;
}

原文标题:HDOJ1042:N!–大数|落絮飞雁的个人网站
原文链接:https://www.luoxufeiyan.com/2015/07/13/hdoj1042/
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。
  1. 大发说道:

    只会在水泥地上走路的人,永远不会留下深深的脚印。

  2. 加百利说道:

    思考是一件最辛苦的工作,这可能是为什么很少人愿意思考的原因。