标签:amp include 类型 ++ names eve des scribe 自带
空格分隔的两个字符串,代表输入的两个大整数
输入的乘积,用字符串表示
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { string a; a[0] = ‘2‘; a[1] = ‘4‘; a[2] = ‘5‘; cout<<a<<endl; //输出为空 cout<<a[1]<<endl; //输出为4 }
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { string a; a[0] = ‘2‘; a[1] = ‘4‘; a[2] = ‘5‘; cout<<a<<endl; //输出为空 string b; b += ‘2‘; b += ‘4‘; b += ‘5‘; cout<<b<<endl; //输出为245 }
#include<iostream> #include<cstdio> #include<string> using namespace std; string Reverse(string ans) { for(int i=0,j=ans.length()-1;i<j;i++,j--) { char temp = ans[i]; ans[i] = ans[j]; ans[j] = temp; } return ans; } string Add(string s1,string s2) { string temp; if(s1.length() < s2.length()) //确保s1长度大于等于s2 { temp = s1; s1 = s2; s2 = temp; } int i=s1.length()-1,j=s2.length()-1; string ans; int t=0; while(i>=0 || j>=0) { if(i>=0 && j>=0) { int a = (s1[i]-‘0‘); int b = (s2[j]-‘0‘); if(a+b+t >= 10) { a = a+b+t-10; ans += (a + ‘0‘); t = 1; } else { a = a+b+t; ans += (a + ‘0‘); t = 0; } i--; j--; } else if(i>=0 && j<0) //保证s1长度>=s2 { int a = (s1[i]-‘0‘) + t; if(a >= 10) { a = a - 10; ans += (a + ‘0‘); t = 1; } else { ans += (a + ‘0‘); t = 0; } i--; } } if(t == 1) { ans += ‘1‘; } ans = Reverse(ans); return ans; } string Mul(string s1,string s2) { string temp; if(s1.length() < s2.length()) //确保s1长度大于等于s2 { temp = s1; s1 = s2; s2 = temp; } int t=0,h=0,num=0; string ans,tmp; for(int i = s2.length()-1;i>=0;i--) { for(int j = s1.length()-1;j>=0;j--) { int a = (s1[j]-‘0‘); int b = (s2[i]-‘0‘); int sum = a*b+t; a = sum % 10; t = sum / 10; tmp += (a+‘0‘); } if(t!=0) { tmp += (t+‘0‘); h++; t = 0; } tmp = Reverse(tmp); for(int i=0;i<num;i++) { tmp += ‘0‘; } num++; ans = Add(tmp,ans); tmp = ""; } return ans; } int main() { string s1,s2; cin>>s1>>s2; string ans; ans = Mul(s1,s2); cout<<ans; return 0; }
标签:amp include 类型 ++ names eve des scribe 自带
原文地址:https://www.cnblogs.com/fzuhyj/p/10688672.html