蓝桥杯- 基础练习:十进制转十六进制

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);
    }
}

 

posted @ 2018-01-14 20:29  Bap  阅读(1408)  评论(0编辑  收藏  举报