文章

5

粉丝

153

获赞

0

访问

4.1k

头像
A+B:
P1326 浙江大学机试题
发布于2023年6月24日 11:43
阅读数 384

// 用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;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发