美文网首页
2020-05-29 阶乘计算

2020-05-29 阶乘计算

作者: JalorOo | 来源:发表于2020-05-29 11:49 被阅读0次
    #include<iostream>
    #include <bitset>
    #include<iomanip>
    #include <algorithm>
    #define PI 3.1415926535898
    #define mod 10007
    using namespace std;
    
    int read(){//普通的快读,不解释
        int x=0,f=1;
        char c=getchar();
        while(c<'0'||c>'9'){
            if(c=='-') f=-1;
            c=getchar();
        }
        while(c>='0'&&c<='9'){
            x=x*10+c-'0';
            c=getchar();
        }
        return x*f;
    }
    
    int A[2605];
    string ans;
    
    void A_Mutiply(int n,bool last){
        //printf("n: %d\n",n);
        bool skip = false;
        for (int i = 0; i<2599; i++) {
            if (skip) {
                skip = false;
            }else{
                A[i]*=n;
            }
            if (A[i]>9) {
                A[i+1] *=n ;
                A[i+1] +=A[i]/10 ;
                skip = true;
                //printf("进位:A[%d] = %d\n",i+1,A[i+1]);
                A[i]%=10;
            }
            //printf("A[%d] = %d\n",i,A[i]);
            if (last) {
                ans+= to_string(A[i]);
                //cout<<ans<<endl;
            }
            //printf("end\n");
        }
    }
    
    int main(){
        A[0] = 1;
        int n = read();
        for (int i = 1; i<=n; i++) {
            if (i==n) {
                //printf("=====");
                A_Mutiply(i, true);
            } else {
                A_Mutiply(i,false);
            }
        }
        reverse(ans.begin(),ans.end());
        for (int i = 0; i<ans.length();) {
            if (ans[i]=='0') {
                ans.erase(i,1);
            } else {
                break;
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:2020-05-29 阶乘计算

          本文链接:https://www.haomeiwen.com/subject/ogjxzhtx.html