题目描述:
Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.
In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.
For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:
20000000000000000 + 4000000000000000 = 24 000 000 000 000 000
20000000000000000 - 4000000000000000 = 16 000 000 000 000 000
20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000
Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.
As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.
输入:
Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).
输出:
For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
样例输入:
20000000000000000
4000000000000000
样例输出:
24000000000000000
16000000000000000
Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.
In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.
For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:
20000000000000000 + 4000000000000000 = 24 000 000 000 000 000
20000000000000000 - 4000000000000000 = 16 000 000 000 000 000
20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000
Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.
As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.
输入:
Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).
输出:
For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
样例输入:
20000000000000000
4000000000000000
样例输出:
24000000000000000
16000000000000000
80000000000000000000000000000000
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool Judge(const string &a, const string &b) //判断字符串a是否大于等于字符串b
{
int len_a = a.size();
int len_b = b.size();
if (len_a == len_b)
return a >= b;
else
return len_a > len_b;
}
string Plus(const string &a, const string &b)
{
int len = max(a.size(), b.size());
vector<int> ivec(len + 1, 0); //最大位数要么是len,要么是len + 1;
string s(len + 1, '0'); //最后的计算结果
for (int i = a.size() - 1, j = b.size() - 1; len > 0; --i, --j, --len)
{
ivec[len] = (i >= 0 ? a[i] - '0' : 0) + (j >= 0 ? b[j] - '0' : 0);
}
for (int i = ivec.size() - 1; i >= 0; --i)
{
if (ivec[i] >= 10)
{
ivec[i - 1] += 1;
}
s[i] = ivec[i] % 10 + '0';
}
if (s[0] == '0')
s.erase(s.begin());
return s;
}
string Minus(const string &a, const string &b)
{
string s;
if (!Judge(a, b))
{
s = Minus(b, a);
s.insert(s.begin(), '-');
}
else
{
int len = max(a.size(), b.size());
vector<int> ivec(len, 0); //最大位数要么是len,要么是len - 1;
s.assign(len, '0'); //最后的计算结果
for (int i = a.size() - 1, j = b.size() - 1; len > 0; --i, --j, --len)
{
ivec[len - 1] = (i >= 0 ? a[i] - '0' : 0) - (j >= 0 ? b[j] - '0' : 0);
}
for (int i = ivec.size() - 1; i >= 0; --i)
{
if (ivec[i] < 0)
{
ivec[i - 1] -= 1;
}
s[i] = (ivec[i] + 10) % 10 + '0';
}
}
string::size_type pos = s.find_first_not_of('0'); //解决差全0的问题
if (pos == string::npos)
s.assign(1, '0');
return s;
}
string Multi(const string &a, const string &b)
{
int len = a.size() + b.size();
vector<int> ivec(len, 0); //最大位数要么是len,要么是len - 1;
string s(len, '0'); //最后的计算结果
for (int i = 0; i != a.size(); ++i)
{
for (int j = 0; j != b.size(); ++j)
{
ivec[i + j + 1] += (a[i] - '0') * (b[j] - '0'); //注意这里是+=
}
}
for (int i = ivec.size() - 1; i >= 0; --i)
{
if (ivec[i] > 10)
{
ivec[i - 1] += ivec[i] / 10; //注意一下
}
s[i] = ivec[i] % 10 + '0';
}
if (s[0] == '0')
s.erase(s.begin());
return s;
}
int main()
{
string a, b;
while (cin >> a >> b)
{
cout << Plus(a, b) << endl;
cout << Minus(a, b) << endl;
cout << Multi(a, b) << endl;
}
return 0;
}