高精度模板

by spark:

#include<iostream>    
#include<cstdio>    
#include<vector>    
#include<cstring>    
#define LL long long     
using namespace std;    
struct sparkint{    
    static const int BASE=1000000000;    
    static const int width=9;    
    vector<LL> s;    
        
    sparkint (LL num=0){*this = num;}    
    sparkint operator = (LL num){    
        s.clear();    
        do{    
            s.push_back(num%BASE);    
            num/=BASE;    
        }while(num>0);    
        return *this;    
    }    
    sparkint operator = (const string& str){    
        s.clear();    
        int x,len=(str.length()-1)/width+1;    
        for(int i=0;i<len;i++){    
            int end=str.length()-i*width;    
            int start=max(0,end-width);    
            sscanf(str.substr(start,end-start).c_str(),"%d",&x);    
            s.push_back(x);    
        }    
        return *this;    
    }    
    //四则运算     
    sparkint operator + (const sparkint &b)const {    
        sparkint c;    
        c.s.clear();    
        for(int i=0,g=0;;i++){    
            if(i>=s.size()&&i>=b.s.size()&&g==0) break;    
            int x=g;    
            if(i<s.size())x+=s[i];    
            if(i<b.s.size()) x+=b.s[i];    
            c.s.push_back(x%BASE);    
            g=x/BASE;    
        }    
        return c;    
    }    
    sparkint operator - (const sparkint &b)const {    
        sparkint c;    
        c.s.clear();    
        for(int i=0,g=0;;i++){    
            if(i>=s.size()&&i>=b.s.size()&&g==0) break;    
            int x=g;    
            if(i<s.size()) x+=s[i];    
            if(i<b.s.size()) x-=b.s[i];    
            if(x<0) g=-1,x+=BASE;  
            else g=0;    
            c.s.push_back(x);    
        }    
        return c;    
    }    
        
    sparkint operator * (const sparkint &b)const {    
        LL i,j,g=0,cur,size=s.size()+b.s.size();
		LL buf[size+5];    
        sparkint c;    
        c.s.clear();  
        memset(buf,0,sizeof(buf));    
        if((*this)==0||b==0) return c;    
        for(i=s.size()-1;i>=0;i--)    
            for(j=b.s.size()-1;j>=0;j--)	
                buf[i+j]+=b.s[j]*s[i];
        for(i=0;i<size;i++){    
            buf[i+1]+=buf[i]/BASE;    
            buf[i]%=BASE;    
        }    
        if(buf[size-1]==0)size--;  
        for(i=0;i<size;i++)c.s.push_back(buf[i]);   
        return c;    
    }    
    sparkint operator / (const sparkint &b)const {    
            
    }    
    sparkint operator % (const sparkint &b)const {    
    }    
    //比较运算符     
    bool operator < (const sparkint &b)const {    
        if(s.size()!=b.s.size()) return s.size()<b.s.size();    
        for(int i=s.size()-1;i>=0;i--)    
            if(s[i]!=b.s[i]) return s[i]<b.s[i];    
        return false;       
    }    
    bool operator > (const sparkint &b)const {    
        return b<(*this);    
    }    
    bool operator == (const sparkint &b)const {    
        return !(*this<b) && !(b< *this);    
    }    
    bool operator <= (const sparkint &b)const {    
        return *this<b || *this == b;    
    }    
    bool operator >= (const sparkint &b)const {    
        return *this>b || *this == b;    
    }    
    bool operator != (const sparkint &b)const {    
        return *this<b || *this > b;    
    }    
    sparkint operator *= (const sparkint &b){
    	*this=(*this)*b; return *this;
	}   
	sparkint operator -= (const sparkint &b){
    	*this=(*this)-b; return *this;
	}
	sparkint operator += (const sparkint &b){
    	*this=(*this)+b; return *this;
	} 
	sparkint operator ++ (int) {
    	 *this=*this+1; return *this;
	} 
	sparkint& operator ++ () {
    	 *this=*this+1; return *this;
	}
	sparkint operator -- (int) {
    	 *this=*this-1; return *this;
	} 
	sparkint& operator -- () {
    	 *this=*this-1; return *this;
	}
};    
    
ostream& operator <<(ostream &out,const sparkint& x){    
    out<<x.s.back();    
    for(int i=x.s.size()-2;i>=0;i--){    
        char buf[20];    
        sprintf(buf,"%09d",x.s[i]);    
        for(int j=0;j<strlen(buf);j++)out<<buf[j];    
    }    
    return out;    
}    
istream&  operator >> (istream &in,sparkint& x){    
    string s;    
    if(!(in>>s)) return in;    
    x=s;    
    return in;    
}    
    
int main(){    
	sparkint ans=1,i,n;
	cin>>n;
	for(i=1;i<=n;i++)ans=ans*i;
	cout<<ans<<endl;
}    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值