高精度模板【高精度加减乘除模带负数判定】
来源:互联网 发布:apache flink hdfs 编辑:程序博客网 时间:2024/05/22 01:39
#include<iostream>#include<cstdio>#include<cstring>const int maxn=500;using namespace std;struct BN{ int e[maxn],len; int syb; BN(){memset(e,0,sizeof e);len=1;syb=1;} BN operator =(const char *a) { int j=0; if(!isdigit(a[0])&&a[0]=='-') syb=-1,len=strlen(a)-1,j++; else syb=1,len=strlen(a); for(int i=1;i<=len;i++,j++) e[len-i+1]=a[j]-'0'; } BN operator =(const int& num) { char a[maxn]; sprintf(a,"%d",num); *this=a; return *this; } BN(const int& num){*this = num;} BN(const char *num){*this = num;} string str() { string res=""; for(int i=1;i<=len;i++) res=char(e[i]+'0')+res; if(res=="")res="0"; else if(syb==-1)res="-"+res; return res; } BN operator -()const{BN res=*this;res.syb=0-syb;return res;} void clean(){while(len>1&&!e[len])len--;} BN operator +(const BN &a)const { BN res;<span style="white-space:pre"></span><span style="white-space:pre"></span>if(syb==1&&a.syb==1); else if(syb==-1&&a.syb==-1){res=-((-*this)+(-a));return res;} else if(syb==1&&a.syb==-1){res=*this-(-a);return res;} else if(syb==-1&&a.syb==1){res=a-(-*this);return res;}<span style="white-space:pre"></span><span style="white-space:pre"></span>res.len=0; int ub=max(len,a.len); for(int i=1,g=0;g||i<=ub;i++) { int x=g; if(i<=len)x+=e[i]; if(i<=a.len)x+=a.e[i]; res.e[++res.len]=x%10; g=x/10; } return res; } BN operator -(const BN &a)const { BN res;<span style="white-space:pre"></span><span style="white-space:pre"></span>if(syb==1&&a.syb==1&&*this>a); else if(syb==-1&&a.syb==1){res=-( (-*this) +a);return res;} else if(syb==1&&a.syb==-1){res=*this+(-a);return res;} else if(syb==-1&&a.syb==-1){res=-a-(-*this);return res;} else if(syb==1&&a.syb==1&&*this<a){res=-(a-*this);return res;}<span style="white-space:pre"></span> res.len=len; for(int i=1,g=0;i<=len;i++) { int x=e[i]; x-=g; if(i<=a.len)x-=a.e[i]; if(x>=0)g=0; else x+=10,g=1; res.e[i]=x; } res.clean(); return res; } BN operator *(const BN &a)const { BN res; res.len=len+a.len; for(int i=1;i<=len;i++) for(int j=1;j<=a.len;j++) res.e[i+j-1]+=e[i]*a.e[j]; for(int i=1;i<=res.len;i++) { res.e[i+1]+=res.e[i]/10; res.e[i]%=10; } res.clean(); if(syb==a.syb)res.syb=1; else res.syb=-1; return res; } BN operator /(const BN &a)const { BN res,f=0; res.len=len; for(int i=len;i>=1;i--) { f=f*10+e[i]; while(f>=a) { f=f-a; res.e[i]++; } } res.clean(); if(syb==a.syb)res.syb=1; else res.syb=-1; return res; } BN operator %(const BN &a)const { BN res=*this/a; res=*this-res*a; return res; } bool operator <(const BN &a)const { if(syb!=a.syb)return syb<a.syb;<span style="white-space:pre"></span>else if(syb==-1) return -*this>(-a);<span style="white-space:pre"></span>if(len!=a.len)return len<a.len; for(int i=len;i>=1;i--) if(e[i]!=a.e[i])return e[i]<a.e[i]; return 0; } bool operator >(const BN &a)const { return a < *this; } bool operator >=(const BN &a)const { return !(*this<a); } bool operator <=(const BN &a)const { return !(*this>a); }};istream& operator >>(istream &in,BN &a){ string s; in>>s; a=s.c_str(); return in;}ostream& operator <<(ostream &out,BN &a){ out<<a.str(); return out;}int main(){ BN a,b; cin>>a>>b; BN c=a*b; cout<<c;}
0 0
- 高精度模板【高精度加减乘除模带负数判定】
- 高精度abcdefg加减乘除模板。
- 高精度加减乘除模板
- 大数高精度加减乘除模板
- 高精度非负数模板
- 支持负数的高精度模板
- 高精度加减乘除
- 高精度加减乘除
- 高精度加减乘除
- 高精度 <加减乘除>
- 高精度加减乘除
- 两非负整数的高精度加减乘除模C++模板
- 大数(高精度)加减乘除取模
- 高精度取模(模板)
- 大数/高精度加减乘除取模[收藏]
- 大数(高精度)加减乘除取模运算
- 大数据/高精度加减乘除
- 【多题合集】高精度加减乘除
- ArrayList:
- 机房收费系统重构——数据库设计
- in-house发布的ipa包,如何触发下载安装
- Android中的子菜单(SubMenu)使用案例
- 方法的分类:
- 高精度模板【高精度加减乘除模带负数判定】
- 索引器:通常用来操作数组中的元素
- Leetcode Kth Largest Element in an Array
- 第38讲-项目二-打豆豆
- spring+hibernate+struts2应用mysql数据库乱码问题
- javascript练习题
- 高效的用户数据报协议 和 可靠的传输控制协议
- spring+hibernate+struts2应用mysql数据库乱码问题
- 嵌入式linux设置时区