//1
#define MAXN 720
struct HP
{
int len,s[MAXN];
HP()
{
memset(s,0,sizeof(s));
len=1;
}
HP operator =(const char *num)
{
len=strlen(num);
for(int i=0;i<len;i++) s[i]=num[len-i-1]-'0';
}
HP operator =(int num)
{
char s[MAXN];
sprintf(s,"%d",num);
*this=s;
return *this;
}
HP(int num) { *this=num;}
HP(const char*num) {*this=num;}
string str()const
{
string res="";
for(int i=0;i<len;i++) res=(char)(s[i]+'0')+res;
if(res=="") res="0";
return res;
}
HP operator +(const HP& b) const
{
HP c;
c.len=0;
for(int i=0,g=0;g||i<max(len,b.len);i++)
{
int x=g;
if(i<len) x+=s[i];
if(i<b.len) x+=b.s[i];
c.s[c.len++]=x%10;
g=x/10;
}
return c;
}
void clean()
{
while(len > 1 && !s[len-1]) len--;
}
HP operator += (const HP& b)
{
*this = *this + b;
return *this;
}
};
istream& operator >>(istream &in, HP& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator <<(ostream &out, const HP& x)
{
out << x.str();
return out;
}
//2
struct BigNum{
char str[maxn]; //数字反着存方便运算
int len;
BigNum():len(0){
memset(str, 0, sizeof str);
}
BigNum operator+(BigNum &num)
{
BigNum ret;
for(int i=0, g=0; g || i<max(len, num.len); i++){
int ts = g;
if(i<len) ts+=(int)str[i]-'0';
if(i<num.len) ts+=(int)num.str[i]-'0';
g = ts/10;
ret.str[ret.len++] = ts%10+'0';
}
ret.str[ret.len] = '\0';
return ret;
}
BigNum operator+(int n)
{
BigNum num, ret;
sprintf(num.str, "%d", n);
num.len = strlen(num.str);
for(int i=0; i<num.len/2; i++){
swap(num.str[i], num.str[num.len-i-1]);
}
ret = num+*this;
return ret;
}
BigNum operator=(char c[]){
BigNum ret;
ret.len = strlen(c);
for(int i=ret.len-1; i>=0; i--){
ret.str[i] = c[i];
}
ret.str[len] = '\0';
return ret;
}
BigNum operator=(int num){
sprintf(this->str, "%d", num);
this->len = strlen(this->str);
for(int i=0; i<this->len/2; i++){
swap(this->str[i], this->str[this->len-i-1]);
}
return *this;
}
};
ostream& operator <<(ostream &out, const BigNum& x){
for(int i=x.len-1; i>=0; i--){
out << x.str[i];
}
return out;
}
istream& operator >>(istream &in, BigNum& x){
in >> x.str;;
x.len = strlen(x.str);
for(int i=0; i<x.len/2; i++){
swap(x.str[i], x.str[x.len-i-1]);
}
return in;
}
高精度模版
最新推荐文章于 2023-09-30 11:15:33 发布