进制转换
[编程题] 进制转换
给定一个十进制数M,以及需要转换的进制数N。将十进制数M转化为N进制数
输入描述:
输入为一行,M(32位整数)、N(2 ≤ N ≤ 16),以空格隔开。
输出描述:
为每个测试实例输出转换后的数,每个输出占一行。如果N大于9,则对应的数字规则参考16进制(比如,10用A表示,等等)
输入例子:
7 2
输出例子:
111
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(); //integer int n = in.nextInt(); //binary String str = ""; boolean flag = false; if(m<=0){ flag = true; m = m *(-1); } while(m>0){ int temp = m%n; if(temp>=10){ temp = temp+55; str = (char)temp + str; }else{ str = temp + str; } m = m/n; } if(flag) str = "-"+str; System.out.println(str); } in.close(); } }
Jumping from failure to failure with undiminished enthusiasm is the big secret to success.