75,2测试点wa,求助大佬
查看原帖
75,2测试点wa,求助大佬
1057068
Faded_wind123楼主2024/4/14 00:24
#include <iostream>
#include <string>

using namespace std;

int hw(string a) {
    int l = a.size();
    for (int i = 0; i < l / 2; i++) {
        if (a[i] != a[l - 1 - i]) {
            return 0;
        }
    }
    return 1;
}

string add(string a, string b, int jz) {
    int l1 = a.size(), l2 = b.size();
    if (l1 > l2) {
        b = string(l1 - l2, '0') + b;
    }
    if (l1 < l2) {
        a = string(l2 - l1, '0') + a;
    }
    int cf = 0, temp = 0;
    string c;
    int f = max(l1, l2);
    for (int i = 0; i < f; i++) {
        temp = a[f - 1 - i] + b[f - 1 - i] + cf - 2 * '0';
        cf = temp / jz;
        temp %= jz;
        c = char(temp + '0') + c;
    }
    if (cf != 0) {
        c = char(cf + '0') + c;
    }
    return c;
}

string change(string a) {
    string b;
    for (int i = a.size() - 1; i >= 0; i--) {
        b += a[i];
    }
    return b;
}

int main() {
    int k;
    string a;
    cin >> k >> a;
    int step = 0;
    
    do {
        if (hw(a)) {
            cout << step;
            return 0;
        }
        
        a = add(a, change(a), k);
        step++;
    } while (step <= 30);
    
    cout << "Impossible!";
    return 0;
}
2024/4/14 00:24
加载中...