有点难度,借鉴了其他同学的,感觉写得很好,略作修改
#include <stdio.h>
#include <math.h>
long long huiwen(long long M, int N) {
    long long out = 0;
    while (M != 0) {
        if (N < 11) {
            out = out * 10 + M % 10;
            M = M / 10;
        } else {
            out = out * 16 + M % 16;
            M = M / 16;
        }
    }
    return out;
}
long long sum(long long M, long long huiwen_M, int N) {
    int num, jin = 0, i = 0;
    long long out = 0;
    while (M != 0) {
        if (N < 11) {
            num = M % 10 + huiwen_M % 10 + jin;
            out = out + num % N * pow(10, i);
            jin = num / N;
            M = M / 10;
            huiwen_M = huiwen_M / 10;
            i++;
        } else {
            num = M % 16 + huiwen_M % 16 + jin;
            out = out + num % N * pow(16, i);
            jin = num / N;
            M = M / 16;
            huiwen_M = huiwen_M / 16;
            i++;
        }
    }
    if (N < 11) out = out + jin * pow(10, i);
    else out = out + jin * pow(16, i);
    return out;
}
int main() {
    int N, step = 0, jud = 1;
    long long M;
    scanf("%d", &N);
    if (N < 11) scanf("%lld", &M);
    else scanf("%X", &M);
    while (M != huiwen(M, N)) {
        if (step > 30) {
            jud = 0;
            break;
        }
        M = sum(M, huiwen(M, N), N);
        step++;
    }
    if (jud == 1) printf("STEP=%d", step);
    else printf("Impossible!");
    return 0;
}