字符串 高精度计算
原因:
因为不喜欢紫书的大数,所以自己写了个能跑的。
大数相加
#include <iostream> #include <vector> #include <cstring> #include <string> using namespace std; int main() { vector<int> c; string s1,s2; cin >> s1 >> s2; int re = 0; int s = 0; int p1,p2; p1 = s1.length()-1; p2 = s2.length()-1; while(p1 >=0 || p2>=0 ) { s = re; if(p1 >= 0) s+= s1[p1--] - '0'; if(p2 >= 0) s+= s2[p2--] - '0'; re = s/10; c.push_back( s%10 ); } if(re) c.push_back(re); for(int i=c.size() - 1; i>0 && c[i]==0; --i) { c.pop_back(); } for(int i=c.size()-1;i>=0;--i){ cout << c[i]; } return 0; }
大数相减
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> using namespace std; int main(){ string s1,s2; vector<int> c; cin >> s1 >> s2; bool neg = false; if( (s1.length() < s2.length()) || ( (s1.length() == s2.length())&&(s1 < s2) ) ){ neg = true; } string &a = (!neg?s1:s2),&b = (!neg?s2:s1); int lena = a.length()-1; int lenb = b.length()-1; int re = 0; int s = 0; while(lena >=0 || lenb >=0){ s = re; re = 0; if(lena >= 0) s+= a[lena--] - '0'; if(lenb >= 0) s-= b[lenb--] - '0'; if(s < 0){ re = -1; s += 10; } c.push_back(s); } for(int i=c.size()-1;i>0 && c[i]==0;--i){ c.pop_back(); } if(neg) cout << "-"; for(int i=c.size()-1;i>=0;--i){ cout << c[i]; } return 0; }
大数相乘
#include <iostream> #include <vector> #include <cstring> #include <string> using namespace std; int main() { vector<int> c; string s1,s2; cin >> s1 >> s2; int s = 0; int re = 0; int lena = s1.length()-1; int lenb = s2.length()-1; for(int i=lena;i>=0;--i) { re = 0; for(int j=lenb;j>=0;--j){ int ii = lena - i;//0~lena int jj = lenb - j;//0~lenb /* [ dis ][ j ][ i ] [ c.size() ] */ int len = c.size(); int dis = len-jj-ii; s = re; s += (s1[i] - '0') * (s2[j] - '0'); if(dis<=0){ c.push_back(s%10); }else{ s += c[len-dis]; c[len-dis] = s%10; } re = s/10; } if(re){ c.push_back(re); } } for(int i=c.size() - 1; i>0 && c[i]==0; --i) { c.pop_back(); } for(int i=c.size()-1;i>=0;--i){ cout << c[i]; } return 0; }
大数相除
高精度除以低精度
#include <iostream> #include <vector> #include <cstring> #include <string> #include <queue> using namespace std; int main() { string s1; deque<int> c; cin >> s1; int b; cin >> b; int x = 0; for(int i=0;i<s1.length();++i){ int t = s1[i] - '0' + x*10; c.push_back( t/b ); x = t%b; } while(c.size() >=1 && c.front() ==0){ c.pop_front(); } for(int i=0;i<c.size();i++){ cout << c[i]; } cout << endl; cout << x; return 0; }
高精度除以高精度
#include <iostream> #include <string> #include <vector> using namespace std; //示范 111111/31 (可以继续优化) //111111 //310000 //18111 //31000 //2611 //3100 //131 //310 bool cmp(const string &s1,const string &s2) {//如果大数 a >= b 返回 true return (s1.length() > s2.length()) || (s1.length()==s2.length() && s1>=s2); } void minus_(string &s1,const string &s2) {//大数相减 string ans; int len1 = s1.length()-1; int len2 = s2.length()-1; int s = 0; int re = 0; while(len1 >=0 || len2 >=0) { s = re; re = 0; if(len1 >=0) s+=s1[len1]-'0'; if(len2 >=0) s-=s2[len2--]-'0'; if(s<0) { re = -1; s+=10; } s1[len1--] = s+'0'; } while( (*s1.begin()) == '0' && s1.length() > 1){ s1.erase(s1.begin()); } } void div(string s1,string s2) { vector<int> ans; int len1 = s1.length(); int len2 = s2.length(); int lend = len1 - len2; for(int i=lend;i>0;--i){ s2.push_back('0'); } int timer = 0; while(lend>=0){ timer = 0; while(cmp(s1,s2)){ minus_(s1,s2); timer++; } s2.erase(s2.end() - 1);//C11 支持pop_back(); ans.push_back(timer); lend--; } for(unsigned int i=0,j=0;i<ans.size();++i){ if(ans[i] > 0){ j = 1; } if(j){ cout << ans[i]; } } cout << endl; cout << s1 << endl; } int main() { string a,b; cin >> a >> b; div(a,b); return 0; }