蓝桥杯- 基础练习:十进制转十六进制
Posted 懒蛋。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯- 基础练习:十进制转十六进制相关的知识,希望对你有一定的参考价值。
package shiti; import java.util.Scanner; //十进制转十六进制 public class D { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(), m = 0; char c; String s = ""; if (n == 0) //如果输入为0 , 那么结果也为0 s = "0"; while (n % 16 != 0 || n / 16 != 0) { m = n % 16; if (m / 10 == 1) c = (char) (65 + m % 10); else c = (char) (48 + m); s = c + s; n = n / 16; } System.out.println(s); } }
package shiti; import java.util.Scanner; public class D1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long nextLong = scanner.nextLong(); StringBuffer string = new StringBuffer(); Long midLong = 0l; char midchar; if (nextLong == 0) { string.append("0"); } while (nextLong / 16 != 0 || nextLong % 16 != 0) { midLong = nextLong % 16; if (midLong / 10 == 1) { midchar = (char) (65 + (midLong % 10)); } else { midchar = (char) (midLong + 48); } string.insert(0, midchar); nextLong /= 16; } System.out.println(string); } }
以上是关于蓝桥杯- 基础练习:十进制转十六进制的主要内容,如果未能解决你的问题,请参考以下文章