码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-14 21:20:49      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:can   int()   ext   print   stringbu   com   idc   gpo   rgs   

技术分享图片

技术分享图片

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

 

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

标签:can   int()   ext   print   stringbu   com   idc   gpo   rgs   

原文地址:https://www.cnblogs.com/wzqjy/p/8284219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!