高精度模板
2016-05-19 18:43
309 查看
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; }
相关文章推荐
- js 获取页面高度和宽度兼容 ie firefox chrome等
- 2015年最新中国知网CNKI免费账号直接入口
- TF-IDF及其算法
- 【SqlServer-无限递归树状图结构设计和查询】
- gitlab安装
- linux安装软件(rpm/yum/apt-get/python)等解决依赖问题
- JAVA多线程和并发基础面试问答
- 玩游戏学编程,寓教于乐:12个学习编程的游戏化平台
- 网易新闻客户端iOS版本中新闻详情页(UIWebView)技术实现的分析探讨
- Spark疑问3之spark 如何并行执行多个Job?
- 大规模IM在线用户的计算和数据存储方案
- 55.按之字形顺序打印二叉树
- VB速查大全(数据库、表格及报表编程) ★ VB错误处理,ado常见错误,VB数据类型等,网上有很多教程是错的,强烈建议看此文
- servlet等一些砸碎的
- Java反射实战
- C语言数据类型杂记
- Java中的锁
- 基础总结篇之五:BroadcastReceiver应用详解
- jQuery 获取屏幕高度、宽度
- 重新设置属主和权限