1. 程式人生 > >XDOJ 1046 - 高精度模板綜合測試 - [高精度模板]

XDOJ 1046 - 高精度模板綜合測試 - [高精度模板]

 題目連結:http://acm.xidian.edu.cn/problem.php?id=1046

題目描述

請輸出兩個數的和,差,積,商,取餘。注意不要有前導零。

輸入

多組資料,每組資料是兩個整數A,B(0<=A<=10^100,0<B<=10^100).

輸出

對於每組資料,輸出五個整數,分別代表A+B,A-B,A*B,A/B,A%B.

樣例輸入
5 2
11 3

樣例輸出
7 3 10 2 1
14 8 33 3 2

 

模板註釋:

本模板只能處理十進位制非負大整數。

 

AC程式碼:

#include<bits/stdc++.h>
using
namespace std; struct BigInt { int len,d[205]; void clean(){while(len>1 && !d[len-1]) len--;} string str()const { string s; for(int i=0;i<len;i++) s+=d[len-1-i]+'0'; return s; } BigInt(){memset(d,0,sizeof(d));len=1;} BigInt(int num){*this
=num;} BigInt(char* num){*this=num;} bool operator<(const BigInt& oth)const { if(len!=oth.len) return len<oth.len; for(int i=len-1;i>=0;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i]; return false; } bool operator>(const BigInt& oth)const
{return oth<*this;} bool operator<=(const BigInt& oth)const{return !(oth<*this);} bool operator>=(const BigInt& oth)const{return !(*this<oth);} bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;} bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);} BigInt operator=(const char* num) { memset(d,0,sizeof(d)); len=strlen(num); for(int i=0;i<len;i++) d[i]=num[len-1-i]-'0'; clean(); return *this; } BigInt operator=(int num) { char s[20]; sprintf(s,"%d",num); return *this=s; } BigInt operator+(const BigInt& oth)const { BigInt c; c.len=max(len,oth.len); for(int i=0;i<=c.len;i++) c.d[i]=0; for(int i=0;i<c.len;i++) { c.d[i]+=(i<len?d[i]:0)+(i<oth.len?oth.d[i]:0); c.d[i+1]+=c.d[i]/10; c.d[i]%=10; } c.len+=(c.d[c.len]>0); c.clean(); return c; } BigInt operator-(const BigInt& oth)const { BigInt c=*this; if(c<oth) printf("Produce negative number!\n"); int i; for(i=0;i<oth.len;i++) { c.d[i]-=oth.d[i]; if(c.d[i]<0) c.d[i]+=10, c.d[i+1]--; } while(c.d[i]<0) c.d[i++]+=10, c.d[i]--; c.clean(); return c; } BigInt operator*(const BigInt& oth)const { BigInt c; for(int i=0;i<len;i++) for(int j=0;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j]; for(int i=0;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+1]+=c.d[i]/10, c.d[i]%=10; c.clean(); return c; } BigInt operator/(const BigInt& oth)const { BigInt c=*this, r=0; for(int i=0;i<len;i++) { r=r*10+c.d[len-1-i]; int j; for(j=0;j<10;j++) if(r<oth*(j+1)) break; c.d[len-1-i]=j; r=r-oth*j; } c.clean(); return c; } BigInt operator%(const BigInt& oth) { BigInt r=0; for(int i=0;i<len;i++) { r=r*10+d[len-1-i]; int j; for(j=0;j<10;j++) if(r<oth*(j+1)) break; r=r-oth*j; } return r; } BigInt operator+=(const BigInt& oth) { *this=*this+oth; return *this; } BigInt operator*=(const BigInt& oth) { *this=*this*oth; return *this; } BigInt operator-=(const BigInt& oth) { *this=*this-oth; return *this; } BigInt operator/=(const BigInt& oth) { *this=*this/oth; return *this; } }; istream& operator>>(istream& in, BigInt& x) { string s; in>>s; x=s.c_str(); return in; } ostream& operator<<(ostream& out,const BigInt& x) { out<<x.str(); return out; } int main() { BigInt a,b; while(cin>>a>>b) { cout<<a+b<<" "; if(a<b) cout<<'-'<<b-a<<" "; else cout<<a-b<<" "; cout<<a*b<<" "; cout<<a/b<<" "; cout<<a%b<<endl; } }