LeetCode483(2021.6.18)

LC483. 最小好进制

class Solution {
public:
    string smallestGoodBase(string n) {
        long long m = stol(n);
        int max = (int)(log(m) / log(2) + 1);
        for (int len = max; len >= 3; --len) {
            long long k = (long long)powl(m, 1.0 / (len - 1));
            long long res = 0;
            for (int i = 0; i < len; ++i) res = res * k + 1;
            if (res == m) return to_string(k);
        }
        return to_string(m - 1);
    }
};

发表评论