是几进制,取多少的模即可

c++实现

class Solution {
public:
    string s = "0123456789ABCDEF";
    string solve(int M, int N) {
        // write code here
        if(M == 0) return "0";
        string res = "";
        bool flag=false; //用来标识正负号
        if(M < 0){
            flag = true;
            M = abs(M);
        }
        while(M){
            res = s[M%N] + res;
            M /= N;
        }
        if(flag){  //如果是负数再补回去
            res = "-" + res;
        }
        return res;
    }
};

python实现

class Solution:
    def solve(self , M: int, N: int) -> str:
        # write code here
        s = "0123456789ABCDEF"
        res = ""
        flag = False;  # 用来保存负号
        if M<0:
            flag = True
            M = abs(M)
        while M:
            res = s[M%N] + res
            M //= N
        if flag:
            res = '-' + res
        return res