题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805263297527808
坑点:
- n小于等于20,也就是说最后的结果可能有21位,比long long还大,因此结果应该由数组存储
- 高位带有0时不会输出,但结果为0时,要输出0
- 位数记得从低位开始计数,因此结果应该反过来输出
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void solve()
{
string s1, s2, s3;
cin>>s1>>s2>>s3;
int n = s1.length();
int le1 = s2.length(), le2 = s3.length();
int len = max(le1, le2);
le1--;
le2--;
n--;
int re[20];
int b = 0;
for(int i = 0; i < len; i++)
{
int a = 0;
if(le1 >= 0)
a += s2[le1--] - '0';
if(le2 >= 0)
a += s3[le2--] - '0';
a += b;
int c;
c = s1[n--] - '0';
if(c == 0)
c = 10;
b = a / c;
a = a % c;
re[i] = a;
}
bool fa = false;
if(b != 0){
cout<<b;
fa = true;
}
for(int i = len - 1; i >= 0; i--)
{
if(fa)
cout<<re[i];
else if(re[i] != 0)
{
cout<<re[i];
fa = true;
}
}
if(!fa)
cout<<0;
cout<<endl;
}
int main()
{
solve();
return 0;
}
20分一般,注意坑点
2018年9月20日 9:57:24