75pts高精度求助

P1015 [NOIP1999 普及组] 回文数

littlesnake @ 2024-09-27 18:43:32

rt。不知道十六进制为什么会错。

# include <bits/stdc++.h>
# define N 1010
# define ll long long

using namespace std;

int n, m, step, lena, lenb, lenc;
int a[N], b[N], c[N];
string s;

int main () {

    cin >> n;
    if (n == 16) {
        cin >> s;
        lenc = s.size ();
        for (int i = 0; i < s.size (); i ++) {
            if (s[i] >= 'A' && s[i] <= 'F') {
                c[i] = (s[lenc - 1 - i] - 'A') + 10;
            }
        }
    } else {
        cin >> m;
        while (m != 0) {
            c[++ lenc] = m % 10;
            m /= 10;
        }
    }
    while (step <= 30) {
        step ++;
        lena = lenc, lenb = lenc;
        for (int i = 1; i <= lena; i ++) a[i] = c[i];
        for (int i = 1; i <= lenb; i ++) b[i] = a[lena + 1 - i];
        for (int i = 1; i <= lenc; i ++) c[i] = 0;
        for (int i = 1; i <= lenc; i ++) {
            c[i] += (a[i] + b[i]);
            c[i + 1] += c[i] / n;
            c[i] %= n;
        } 
        if (c[lenc + 1] != 0) lenc ++;
        bool flag = true;
        for (int i = 1; i <= lenc; i ++) {
            if (c[i] != c[lenc + 1 - i]) {
                flag = false;
                break;
            }
        }
        if (flag) break;
    }
    if (step <= 30) cout << "STEP=" << step;
    else cout << "Impossible!";

    return 0;

}

|