The meaning of problems and ideas
The meaning of problems: two input numbers n, r, the n (decimal number) is converted to hexadecimal r.
Thinking:% r can be converted operation.
Stepped pit points: First, n there may be negative, if it is negative, negative attention to the situation. Second, when the converted number is greater than 10, exp: 10 -> A. Convert it to the corresponding form of hexadecimal numbers each. Particular attention (to my own question), when the output of a character (eg A), not to "rush" output a newline, otherwise it will step my footsteps. Hey!
Code
package com.kyrie.java1; import java.util.ArrayList; import java.util.Scanner; public class HD2031 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, r; while (in.hasNext()) { n = in.nextInt(); r = in.nextInt(); trans(n, r); } in.close(); } private static void trans(int n, int r) { int t = 0; boolean flag = false; if (n < 0) { flag = true; n = -n; } ArrayList<Integer> aL = new ArrayList<Integer>(); // translate while (n != 0) { aL.add(n % r); n /= r; } // 判断负号 if (flag) { System.out.print("-"); } for (int i = aL.size() - 1; i >= 0; i--) { t = aL.get(i); if (t >= 10) { System.out.printf("%c", 'A' + (t - 10)); } else { System.out.print(t); } } System.out.println(); } }