JAVA版 《进制转换》
进制转换
http://www.nowcoder.com/questionTerminal/2cc32b88fff94d7e8fd458b8c7b25ec1
算法思路: 除N取余,然后倒序排列,高位补零。
public String solve (int M, int N) { // write code here if(M == 0) return "0"; String s = "0123456789ABCDEF"; StringBuffer sb = new StringBuffer(); boolean f = false; if(M < 0){ f = true; M = -M; } while(M != 0){ sb.append(s.charAt(M%N)); M /= N; } if(f) sb.append("-"); return sb.reverse().toString(); } }