这个题做的我真的是无语了,第一次提交了17次!
第一次只跑出来了一般,15分。
PTA写出来还是蛮容易的,但比较难的是拿全分!
有时候经常有只有一两个测试点没过了清况,但是就是那个情况,让你拿不到全分!。
我少写了一个等号,导致测试点3一直没过,所以我只能说疯狂测试,找到不能能过的数据,然后修改的代码。。。。
参考测试点3用例如下:
输入:
30527
29416
00001
输出:
100000
修修改改过后勉强AC的代码如下:
#include<string>
#include<iostream>
#include<math.h>
using namespace std;
bool Iszero(string s) {
int num = s[0] - '0';
for (int i = 1; i <= s.length() - 1; i++) {
num += (s[i] - '0') * pow(10,i);
}
if (num == 0)return true;
else return false;
}
int main() {
int Jinzhi, a, b, jinwei=0, benwei;
string s, s1, s2, s3;
getline(cin, s);
getline(cin, s1);
getline(cin, s2);
while (s1.length() && s2.length()) {
Jinzhi = s[s.length() - 1] - '0';
if (Jinzhi == 0)Jinzhi = 10;
a = s1[s1.length() - 1] - '0';
b = s2[s2.length() - 1] - '0';
benwei = (a + b+jinwei) % Jinzhi;
jinwei = (a + b+jinwei) / Jinzhi;
s3.push_back(benwei+'0');
s1.pop_back(); s2.pop_back(); s.pop_back();
}
if (!s1.length() && !s2.length()) {
if (jinwei != 0)s3.push_back(jinwei + '0');
}
while(s1.length()) {
Jinzhi = s[s.length() - 1] - '0';
if (Jinzhi == 0)Jinzhi = 10;
s3.push_back((s1[s1.length()-1] - '0'+jinwei) % Jinzhi+'0');
jinwei = (s1[s1.length() - 1] - '0' + jinwei)/Jinzhi;
s.pop_back(); s1.pop_back();
if (jinwei != 0 && s1.length() == 0)s3.push_back(jinwei + '0');
if (s1.length() == 0 && s3[s3.length() - 1] == '0')s3.pop_back();
}
while (s2.length()) {
Jinzhi = s[s.length() - 1] - '0';
if (Jinzhi == 0)Jinzhi = 10;
s3.push_back(((s2[s2.length() - 1]-'0'+jinwei)) % Jinzhi+'0');
jinwei = (s2[s2.length() - 1] - '0' + jinwei)/ Jinzhi;
s.pop_back(); s2.pop_back();
if (jinwei != 0 && s2.length() == 0)s3.push_back(jinwei + '0');
if (s2.length() == 0 && s3[s3.length() - 1] == '0')s3.pop_back();
}
if (Iszero(s3))cout << "0";
else {
while (s3[s3.length()-1] == '0') {
s3.pop_back();
}
for (int i = s3.length(); i >= 1; i--) {
cout << s3[i - 1];
}
}
return 0;
}
转载:https://blog.csdn.net/ZiYuTongXue/article/details/101705785
查看评论