题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/2cc32b88fff94d7e8fd458b8c7b25ec1
import java.util.*; public class Solution { /** * 进制转换 * @param M int整型 给定整数 * @param N int整型 转换到的进制 * @return string字符串 */ public String solve (int M, int N) { // write code here StringBuilder s = new StringBuilder(); String str = "0123456789ABCDEF"; boolean flag = false; if(M<0) { M = -M; flag = true; } if(M == 0) { s.append("0"); } while(M!=0) { s.append(str.charAt(M%N)); M/=N; } if(flag) { s.append("-"); } s.reverse(); return s.toString(); } }