• 蓝桥杯java 基础训练 十六进制转八进制


    问题描述
      给定n个十六进制正整数,输出它们对应的八进制数。

    输入格式
      输入的第一行为一个正整数n (1<=n<=10)。
      接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。

    输出格式
      输出n行,每行为输入对应的八进制正整数。

      【注意】
      输入的十六进制数不会有前导0,比如012A。
      输出的八进制数也不能有前导0。

    样例输入
      2
      39
      123ABC

    样例输出
      71
      4435274

    一、

    import java.util.*;
    public class Main{
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            String[] sts = new String[n];
            for(int i=0;i<n;i++){
                sts[i] = sc.next();
            }
            sc.close();
            for(int i=0;i<n;i++){
                String strBinary = toBinary(sts[i]);
                int len_strBin = strBinary.length();
                if(len_strBin%3==1) strBinary = "00"+strBinary;
                if(len_strBin%3==2) strBinary = "0"+strBinary;
                String strOctal = toOctal(strBinary);
                System.out.println(strOctal);
            }
        }
        private static String toOctal(String strBinary) {
            int len = strBinary.length();
            int k;
            StringBuffer stb = new StringBuffer();
            if(strBinary.substring(0, 3).equals("000"))
                k=3;
            else
                k=0;
            for(int i=k;i<len-2;i+=3){
                switch (strBinary.substring(i, i+3)) {
                    case "000":stb.append("0");break;
                    case "001":stb.append("1");break;
                    case "010":stb.append("2");break;
                    case "011":stb.append("3");break;
                    case "100":stb.append("4");break;
                    case "101":stb.append("5");break;
                    case "110":stb.append("6");break;
                    case "111":stb.append("7");break;
                    default:break;
                }
            }
            return stb.toString();
        }
        private static String toBinary(String strHex) {
            int len_str = strHex.length();
            StringBuffer stb = new StringBuffer();
            for(int i=0;i<len_str;i++){
                switch (strHex.charAt(i)) {
                    case '0':stb.append("0000");break;
                    case '1':stb.append("0001");break;
                    case '2':stb.append("0010");break;
                    case '3':stb.append("0011");break;
                    case '4':stb.append("0100");break;
                    case '5':stb.append("0101");break;
                    case '6':stb.append("0110");break;
                    case '7':stb.append("0111");break;
                    case '8':stb.append("1000");break;
                    case '9':stb.append("1001");break;
                    case 'A':stb.append("1010");break;
                    case 'B':stb.append("1011");break;
                    case 'C':stb.append("1100");break;
                    case 'D':stb.append("1101");break;
                    case 'E':stb.append("1110");break;
                    case 'F':stb.append("1111");break;
                    default:break;
                }
            }
            return stb.toString();
        }
    }
    

    二、  

    import java.util.HashMap;
    import java.util.Scanner;
    
    public class Test {
        static String[] Binary = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
        static String[] Octal = {"000","001","010","011","100","101","110","111"};
        static HashMap<String, String> map;
        /*
         * Binary是0-15的二进制数的数组
         * Octal是0-7的二进制数的水族
         * map是0-7与其二进制数的映射
         */
    
        //16进制转2进制方法
        public static String HexToBinary(String hex){
            StringBuffer binary = new StringBuffer();
            for ( int i = 0 ; i < hex.length() ; i++){
                char ch = hex.charAt(i);
                if ( ch >= '0' && ch <= '9' ){
                    binary.append(Binary[(int)(ch-'0')]);
                }else{
                    binary.append(Binary[(int)(ch-'A')+10]);
                }
            }
            return binary.toString();
        }
    
        //2进制转8进制方法
        public static String BinaryToOctal(String binary){
            StringBuffer octal = new StringBuffer();
            int reminder = binary.length() % 3;
            //sub为2进制数需要补充0的个数
            int sub = 0;
            if ( reminder != 0)
                sub = 3 - reminder;
            StringBuffer tmp = new StringBuffer();
            for ( int i = 0 ; i < sub ; i++){
                tmp.append("0");
            }
            tmp.append(binary);
            for ( int i = 0 ; i <= tmp.length() - 3 ; i += 3){
                String substring = tmp.substring(i, i+3);
                octal.append(map.get(substring));
            }
            int index = FindZero(octal);
            /*
             * 如果index为-1,则8进制数前面没有0,
             * 反之则有0,取无0的子字符串。
             */
            if(index == -1)
                return octal.toString();
            else
                return octal.substring(index+1).toString();
        }
    
        //统计8进制数的字符串的最前面连续0的个数,返回的最后一个0的下标
        public static int FindZero(StringBuffer octal){
            int index = -1;
            for ( int i = 0 ; i < octal.length() ; i++){
                if ( octal.charAt(i) == '0'){
                    index = i;
                    continue;
                }else{      
                    break;
                }
            }
            return index;
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            map = new HashMap<String, String>();
            for ( int i = 0 ; i < Octal.length ; i++){
                char[] ch = new char[1];
                ch[0] = (char) (i+'0');
                String str = new String(ch);
                map.put(Octal[i], str);
            }
            int num = in.nextInt();
            for (int i = 0 ; i < num ; i++){
                String hex = in.next();
                String binary = HexToBinary(hex);
                String octal = BinaryToOctal(binary);
                System.out.println(octal);
            }
            in.close();
        }
    
    }
    

    三、  

    import java.util.Scanner;
    
    public class Test1 {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		String[] st = new String[n];
    		for (int i = 0; i < n; i++) {
    			st[i] = sc.next();
    		}
    		sc.close();
    		for (int i = 0; i < n; i++) {
    			String str1 = ttos(st[i]);
    
    			int len_str1 = str1.length();
    			if (len_str1 % 3 == 1)
    				str1 = "00" + str1;
    			else if (len_str1 % 3 == 2)
    				str1 = "0" + str1;
    			ttoe(str1);
    			System.out.println();
    		}
    
    	}
    
    	public static String ttos(String str) {
    		int len_str = str.length();
    		StringBuilder str2 = new StringBuilder();
    		for (int i = 0; i < len_str; ++i) {
    			switch (str.charAt(i)) {
    			case '0':
    				str2.append("0000");
    				break;
    			case '1':
    				str2.append("0001");
    				break;
    			case '2':
    				str2.append("0010");
    				break;
    			case '3':
    				str2.append("0011");
    				break;
    			case '4':
    				str2.append("0100");
    				break;
    			case '5':
    				str2.append("0101");
    				break;
    			case '6':
    				str2.append("0110");
    				break;
    			case '7':
    				str2.append("0111");
    				break;
    			case '8':
    				str2.append("1000");
    				break;
    			case '9':
    				str2.append("1001");
    				break;
    			case 'A':
    				str2.append("1010");
    				break;
    			case 'B':
    				str2.append("1011");
    				break;
    			case 'C':
    				str2.append("1100");
    				break;
    			case 'D':
    				str2.append("1101");
    				break;
    			case 'E':
    				str2.append("1110");
    				break;
    			case 'F':
    				str2.append("1111");
    				break;
    			default:
    				break;
    			}
    		}
    		return str2.toString();
    	}
    
    	public static void ttoe(String str2) {
    		int len = str2.length();
    		int a;
    		a = (str2.charAt(0) - '0') * 4 + (str2.charAt(1) - '0') * 2 + (str2.charAt(2) - '0');
    		if (a != 0)
    			System.out.print(a);
    		for (int i = 3; i <= len - 2; i += 3) {
    			a = (str2.charAt(i) - '0') * 4 + (str2.charAt(i + 1) - '0') * 2 + (str2.charAt(i + 2) - '0');
    			System.out.print(a);
    		}
    	}
    }
    

      

  • 相关阅读:
    Unity 移动端的复制这么写
    Unity如何管理住Android 6.0 调皮的权限
    谷歌商店Apk下载器
    Unity编辑器下重启
    git pull error
    如何简单的实现新手引导之UGUI篇
    linux系统安装python3.5
    Grafana设置mysql为数据源
    hyper -v 虚拟机(win_server)忘记密码重置
    zabbix报错:the information displayed may not be current
  • 原文地址:https://www.cnblogs.com/duanyingkui/p/8341821.html
Copyright © 2020-2023  润新知