1057 N的阶乘 【数论】

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 收藏
 关注
输入N求N的阶乘的准确值。
 
Input
输入N(1 <= N <= 10000)
Output
输出N的阶乘
Input示例
5
Output示例
120
模拟平时int类型对10取余进位,输出还需要注意格式,%013lld是不足13位左补0.
#include<stdio.h>
#define mod 10000000000000
#define N 1000010
#define LL long long
LL num[N];

int main()
{
    LL i,j,n;
    LL k,ans,t;
    while(scanf("%lld",&n)!=EOF)
    {
        ans = 1;
        num[1] = 1;
        for(i = 1; i <= n; i ++)
        {
            k = 0;
            for(j = 1; j <= ans; j ++)
            {
                t = num[j]*i + k;
                num[j] = t%mod;
                k = t/mod;//k保存进位数 
            }
            if(k)//如果最后还需要进位 
                num[++ans] = k;
        }
        printf("%lld",num[ans]);
        for(i = ans-1; i >= 1; i --)
            printf("%013lld",num[i]);
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hellocheng/p/7447436.html