题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/ac61207721a34b74b06597fe6eb67c52
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int m = in.nextInt(); int n = in.nextInt(); StringBuilder s = new StringBuilder(); String table = "0123456789ABCDEF"; //用来存储2-16进制中所有的可能 boolean flag = false; //标记下有没有加负号 if(m < 0){ m = -m; flag = true; //本身是负数,我们改为正数了。标记下 } if(m == 0){ s.append('0'); } while (m != 0) { s.append(table.charAt(m % n)); m /= n; } if(flag){ s.append('-'); } s.reverse(); System.out.println(s); } } }
#每日一题##福大大架构师每日一题#