Sicily 1381 a*b

来源:互联网 发布:mac os 网络恢复 编辑:程序博客网 时间:2024/05/22 15:39

高精度乘法

#include <iostream>#include <string>using namespace std;string add(string a, string b) {if (a.length()<b.length())a.swap(b);int carry=0;string::reverse_iterator aIter=a.rbegin();string::reverse_iterator bIter=b.rbegin();while(bIter!=b.rend()) {int tmp=(*aIter)-'0'+(*bIter)-'0'+carry;(*aIter)=tmp%10+'0';carry=tmp/10;aIter++;bIter++;}if (carry==1) {while (aIter!=a.rend())if ((*aIter!='9'))break;else(*aIter++)='0';if (aIter!=a.rend())(*aIter)+=1;elsea.insert(aIter.base(), '1');}return a;}int main() {int cases;cin>>cases;while(cases--) {string a;string b;cin>>a>>b;string result="0";for (int i=1; i<=b[0]-'0'; i++)result=add(result, a);for (int i=1; i<b.length(); i++) {if (result!="0")result+='0';for (int j=1; j<=b[i]-'0'; j++)result=add(result, a);}cout<<result<<"\n";}return 0;}// by wbchou// Feb 19th, 2013


原创粉丝点击