分享

string乘法

 Lukies_图书馆 2019-10-13

#include <iostream>

#include<string>

#include<algorithm>

using namespace std;

string multiply(string a,string b){

string c;

int i=0,j=0;int carry=0,temp=0;

for(i=0;i<a.length();i++){

        carry=0;

for(j=0;j<b.length();j++){

cout<<(a[a.length()-1-i]-48)<<'x'<<(b[b.length()-1-j]-48)<<' ';

temp=(a[a.length()-1-i]-48)*(b[b.length()-1-j]-48)+carry;

if(temp>9){carry=temp/10;temp=temp%10;}

else{carry=0;}

if(i+j==c.length()){c.push_back(temp+48);}

else{c[i+j]+=temp;

if(c[i+j]>57){c[i+j]-=10;carry+=1;}

}

 cout<<c<<endl;

}

if(carry){

   if(i+j==c.length()){c.push_back(carry+48);}

   }

   cout<<c<<endl;

}

reverse(c.begin(),c.end());

return c;

}

int main()

{

 string a,b;cin>>a>>b;

   cout << multiply(b,a);

   return 0;

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多