高精度模板1.0
来源:互联网 发布:nginx tomcat集群配置 编辑:程序博客网 时间:2024/05/29 09:27
注意:本版本的高精度模板只支持整数的输入输出,加法,减法(大数减小数),大小比较。
#include<cstdio> #include<iostream> #include<vector> #include<sstream> #include<cstring> using namespace std; struct bigint{ static const int base=100000000; static const int width=8; vector<int>s; bigint (long long num=0){*this=num;} bigint operator = (long long num){ s.clear(); do{ s.push_back(num%base); num/=base; }while(num>0); return *this; } bigint 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; } bigint operator + (const bigint& b) const{ bigint c; c.s.clear(); for(int i=0,g=0;;i++){ if(g==0&&i>=s.size()&&i>=b.s.size())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; } bigint operator - (const bigint& b) const{ bigint c; c.s.clear(); for(int i=0,g=0;;i++){ if(g==0&&i>=s.size())break; int x=g; if(i<s.size())x+=s[i]; if(i<b.s.size())x-=b.s[i]; if(x<0)x+=base,g=-1; else g=0; c.s.push_back(x); } return c; } bool operator < (const bigint&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 bigint&b) const{return b<*this;} bool operator <= (const bigint&b) const{return !(b<*this);} bool operator >= (const bigint&b) const{return !(*this>b);} bool operator != (const bigint&b) const{return b<*this|| *this<b;} bool operator == (const bigint&b) const{return !(b<*this)&&!(*this<b);} }; ostream& operator << (ostream &out,const bigint& x){ out<<x.s.back(); for(int i=x.s.size()-2;i>=0;i--){ char buf[20]; sprintf(buf,"%08d",x.s[i]); for(int j=0;j<strlen(buf);j++)out<<buf[j]; } return out; } istream& operator >> (istream &in,bigint&x){ string s; if(!(in>>s))return in; x=s; return in; } int main(){ bigint a,b; }
0 0
- 高精度模板1.0
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 【模板】高精度
- 高精度模板
- 基础总结篇之七:ContentProvider之读写短消息
- 面试中的排序算法总结
- 屏幕适配全攻略(二)-- 解决方案
- Eclipse 插件安装方法
- DIY:用开源软件搭建自己的物联网
- 高精度模板1.0
- 变位词(兄弟字符串)
- 01 背包
- centos linux 服务器时间少8个小时的问题
- Linux下的find命令
- Ubuntu相关学习笔记
- 固定ip 与 路由算法
- CoreData VS Realm (2016-02-23更新)
- 网络信息安全攻防实验室 脚本关第二关