文章
5
粉丝
153
获赞
1
访问
5.8k
// 用string判断末尾K个数是否相同会比辗转相除更清晰
#include <iostream>
#include <string>
using namespace std;
int main() {
int A, B, K;
while(cin >> A >> B >> K) {
if(A == 0 && B == 0) break; // End the loop if A and B are both 0
string strA = to_string(A);
string strB = to_string(B);
int lenA = strA.size();
int lenB = strB.size();
bool isSame = true;
for (int i = 0; i < K; i++) {
char digitA = (lenA - 1 - i >= 0) ? strA[lenA - 1 - i] : '0';
char digitB = (lenB - 1 - i >= 0) ? strB[lenB - 1 - i] : '0';
if (digitA != digitB) {
isSame = false;
break;
}
}
if (isSame) {
cout << -1 << endl;
} else {
cout << A + B << endl;
}
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发