ps:以下的大数只考虑正数!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string sum(string s1,string s2) { if(s1.length()<s2.length()) { string temp=s1; s1=s2; s2=temp; } int i,j; for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) { s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); if(s1[i]-'0'>=10) { s1[i]=char((s1[i]-'0')%10+'0'); if(i) s1[i-1]++; else s1='1'+s1; } } return s1; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string sub(string a,string b) { string c; bool ok=0; int lena=a.length(),lenb=b.length(); int len=max(lena,lenb); for(int i=lena;i<len;i++) a="0"+a; for(int i=lenb;i<len;i++) b="0"+b; if(a<b) { string temp=a; a=b; b=temp; ok=1; } for(int i=len-1;i>=0;i--) { if(a[i]<b[i]) { a[i-1]-=1; a[i]+=10; } char temp=a[i]-b[i]+'0'; c=temp+c; } int pos=0; while(c[pos]=='0' && pos<len) pos++; if(pos==len) return "0"; if(ok) return "-"+c.substr(pos); return c.substr(pos); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//大数加法 string sum(string s1,string s2) { if(s1.length()<s2.length()) { string temp=s1; s1=s2; s2=temp; } int i,j; for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) { s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); if(s1[i]-'0'>=10) { s1[i]=char((s1[i]-'0')%10+'0'); if(i) s1[i-1]++; else s1='1'+s1; } } return s1; } //大数乘以整形数 string mulint(string s,int x) { reverse(s.begin(),s.end()); int cmp=0; for(int i=0;i<s.size();i++) { cmp=(s[i]-'0')*x+cmp; s[i]=(cmp%10+'0'); cmp/=10; } while(cmp) { s+=(cmp%10+'0'); cmp/=10; } reverse(s.begin(),s.end()); return s; } //大数乘法 string mul(string x,string y) { string ans; for(int i=y.size()-1,j=0;i>=0;i--,j++) { string tmp=mulint(x,y[i]-'0'); for(int k=0;k<j;k++) tmp+='0'; ans=sum(ans,tmp); } return ans; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string div(string s,int x) { int cmp=0,ok=0; string ans=""; for(int i=0;i<s.size();i++) { cmp=(cmp*10+s[i]-'0'); if(cmp>=x) { ok=1; ans+=(cmp/x+'0'); cmp%=x; } else{ if(ok==1) ans+='0'; } } return ans; }