C++大数阶乘

来源:互联网 发布:铁血皇城辅助软件 编辑:程序博客网 时间:2024/06/18 18:52
#include<bits/stdc++.h>using namespace std;long long res[10002];const long long  d=1e+9;int main(){    long long num;    long long val,temp1,temp2;    while(scanf("%d",&num)!=EOF){        long long c=0;        if(num==0||num==1) printf("1\n");        else{            res[0]=1;            val=1;            for(int i=2;i<=num;i++){                for(int j=0;j<val;j++){                    temp1=res[j]*i+c;                    if(temp1<d){                         res[j]=temp1;                         c=0;                    }                    else{                        res[j]=temp1%d;                        c=temp1/d;                    }                   }                 while(c!=0){                    res[val++]=c%d;                    c/=d;                }               }            printf("%d",res[val-1]);            for(int i=val-2;i>=0;i--) printf("%09lld",res[i]);            printf("\n");        }    }    return 0;}
0 0
原创粉丝点击