首页 > 其他 > 详细

编程习题3

时间:2014-04-11 09:35:14      阅读:504      评论:0      收藏:0      [点我收藏+]

输入一个十六进制,将其转换为三进制

bubuko.com,布布扣
public class Test10 {
    public static void main(String[] args) {
        String num = "A";
        StringBuffer result = new StringBuffer("");
        long n = 0;
        long k = 1;
        for(int i = num.length() -1; i >= 0;i--){
            int ch = num.charAt(i);
            if(ch >= ‘0‘ && ch <=‘9‘){
                ch -= ‘0‘;
            }else if(ch >= ‘A‘ && ch <= ‘F‘){
                ch -= (‘A‘-10);
            }else
                return;
            n +=(ch*k);
            k *= 16;
        }
        for(;n != 0; n /=3){
            result.append(n%3);
        }
        result = result.reverse();
        System.out.println(result);
    }
}
bubuko.com,布布扣

 

 

编程习题3,布布扣,bubuko.com

编程习题3

原文:http://www.cnblogs.com/YESheng/p/3657618.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!