实现大数的加减乘除和取余
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <vector> #include <stack> #include <algorithm> #include <set> #include <cstring> using namespace std; class Biginter { public: string delezero(string a); string add(string a,string b); string sub(string a,string b); string mulit(string a,string b); string divid(string a,string b); string mod(string a,string b); }; string Biginter::delezero(string ans) { int i = 0; while(ans[i] == '0') { i++; } if(i == (int)ans.size()) { return "0"; } return ans.substr(i,ans.size()-i); } string Biginter::add(string a,string b) { string ans = ""; int l1 = a.size()-1; int l2 = b.size()-1; int t = 0; while(l1>=0&&l2>=0) { int sum = t+a[l1]-'0'+b[l2]-'0'; if(sum > 9) { t = 1; sum %= 10; } else { t = 0; } ans += sum + '0'; sum = 0; l1--; l2--; } while(l1>=0) { int sum = a[l1]-'0'+t; if(sum > 9) { t = 1; sum %= 10; } else { t = 0; } l1--; ans += sum + '0'; } while(l2>=0) { int sum = b[l2]-'0'+t; if(sum > 9) { t = 1; sum %= 10; } else { t = 0; } ans += sum + '0'; sum = 0; l2--; } if(t) { ans += t+'0'; } reverse(ans.begin(),ans.end()); return delezero(ans); }; string Biginter::sub(string a,string b) { string ans = ""; if(a==b) { return ans+'0'; } int l1 = a.size()-1; int l2 = b.size()-1; int t = 0; while(l1>=0&&l2>=0) { int sum = a[l1]-'0'-(b[l2]-'0')+t; if(sum < 0) { t = -1; sum += 10; } else { t = 0; } ans += sum+'0'; l1--; l2--; } while(l1>=0) { int sum = a[l1]-'0'+t; if(sum < 0) { t = -1; sum += 10; } else { t = 0; } ans += sum+'0'; l1--; } while(l2>=0) { int sum = -(b[l2]-'0')+t; if(sum < 0) { t = -1; sum += 10; } else { t = 0; } ans += sum+'0'; l2--; } reverse(ans.begin(),ans.end()); return delezero(ans);; }; string Biginter::mulit(string a,string b) { string ans = ""; int l1=a.size()-1; int l2=b.size()-1; int c[10005]; memset(c,sizeof(c)); for(int i=l1,m=0; i>=0; i--,m++) { for(int j=l2,n=0; j>=0; j--,n++) { c[n+m] += (a[i]-'0')*(b[j]-'0'); } } int t = 0; for(int i = 0; i < l1+l2+3; i++) { c[i] += t; if(c[i] >= 10) { t = c[i]/10; c[i] %= 10; } else { t = 0; } ans += c[i]+'0'; } reverse(ans.begin(),ans.end()); return delezero(ans); }; string Biginter::divid(string a,string b) {//a>b string ans = ""; int l1 = a.size(); int l2 = b.size(); string bei = a.substr(0,l2-1); for(int i = l2-1; i < l1; i++) { bei += a[i]; int t = 0; //cout << bei << " = " << b <<endl; int flag = 1; for(int i = 0;i < bei.size();i++){ if(bei[i] != '0'){ flag = 0; break; } } if(flag){ ans += '0'; continue; } while((bei.size() > b.size())||(bei.size()==b.size()&& bei>= b)) { t++; bei = sub(bei,b); } ans += t+'0'; } return delezero(ans); }; string Biginter::mod(string a,string b) { string ans = ""; ans = divid(a,b); ans = mulit(ans,b); ans = sub(a,ans); return delezero(ans); }; int main() { string a,b; Biginter bigint; while(cin >> a >> b) { cout <<bigint.mod(a,b)<<endl; } return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。