#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string s1 = "";
string s2 = "";
string product = "";
//windows Ctrl+Z 和 回车 结束输入
//linux Ctrl+D 结束输入
while (cin >> s1 >> s2)
{
//cin >> s1 >> s2;
//s1 = "98765432123";
//s2 = "12345678987";
int len1 = s1.size();
int len2 = s2.size();
int carry = 0;
int remain = 0;
product.resize(len1 + len2);//乘积的最大长度
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
int tmp = (s1[i] - '0') * (s2[j] - '0') + product[i+j];
carry = tmp / 10;
remain = tmp % 10;
product[i + j + 1] += carry;
product[i + j] = remain;
}
}
if (product[len1+len2-1]==0)//最后没有进位,最高位是0
product.resize(product.size() - 1);
reverse(product.begin(), product.end());
for (int i = 0; i < product.size(); i++)
{
cout << char(product[i] + '0');
}
cout << endl;
//---------------------------------------------
s1 = "";
s2 = "";
product = "";
}
return 0;
}
长整数相乘
最新推荐文章于 2020-06-20 18:28:44 发布