51nod1057 N的阶乘

来源:互联网 发布:通联数据股份公司电话 编辑:程序博客网 时间:2024/05/17 16:56

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057

这题里压位要压的狠一点。最少压四位,少了不行

压位: 每个数组元素存多位数字

#include <iostream>#include <stdio.h>#include <cstring>using namespace std;long long a[1000005];int main(){    int n;    cin>>n;    int cot=0;    a[cot]=1;    for(int i=2;i<=n;i++)    {        long long  step=0;        for(int j=0;j<=cot;j++)        {           a[j]=a[j]*i+step;           step=a[j]/10000 ;           a[j]%=10000 ;        }        if(step)            a[++cot]=step;    }    printf("%lld",a[cot]);    for(int i=cot-1;i>=0;i--)        printf("%04lld",a[i]);    return 0;}


0 0
原创粉丝点击