首页 > 编程知识 正文

蓝桥杯十六进制转八进制c语言,蓝桥杯16进制转8进制c语言

时间:2023-05-03 21:11:40 阅读:239831 作者:2513


这道题其实挺难的,之前做过一次,用的十六进制转化为十进制,然后用十进制再转化为八进制,代码很简单,用的jdk上自带的进制转换进行换算。

import java.util.Scanner;/** * @author 作者 E-mail: WYJ 1627407425@qq.com* @version 创建时间:2019年7月22日 下午4:19:18 * 类说明 十六进制转十进制————十进制转八进制,失败。。。*/public class 十六进制转八进制 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();String[] str = new String[n];long[] lo = new long[n];String[] st = new String[n];for(int i=0;i<n;i++) {str[i] = sc.next();lo[i] = Long.parseLong(str[i],16);st[i] = Long.toOctalString(lo[i]);}for(int i=0;i<n;i++)System.out.println(st[i]);}}

很容易就想到,这个方法就不行,因为十六进制数长度不超过100000这个是长度,而且转化为十进制,就算使用long类型的也不够用,所以这个方法就失败了。
后来在网上看了许多代码,基本上都是差不多的思路,将十六进制转化为二进制,二进制再转化为八进制,这样就行了,开始我还在疑问,十进制都不够用,二进制不应该更不够用,其实这里的二进制不是数字类型,而是字符串类型的,字符串可以储存很长啊。

import java.util.Scanner;/** * @author 作者 E-mail: WYJ 1627407425@qq.com* @version 创建时间:2019年7月22日 下午8:48:56 * 类说明 十六进制转二进制————二进制转八进制。*/public class 十六进制转十进制1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();String[] str = new String[n];for(int i=0;i<n;i++) {str[i] = sc.next();}for(int i=0;i<n;i++) {String st = toBinary(str[i]);int len = st.length();if(len%3==1)st = "00"+st;else if(len%3==2)st = "0"+st;String s = toOctal(st);System.out.println(s);}}private static String toOctal(String st) {StringBuffer buf = new StringBuffer();int k = 0;if(st.substring(0, 3).equals("000")) k=3;else k=0;for(int i=k;i<st.length()-2;i+=3) {//这里的st.length()-2还不是很理解,如果有知道的麻烦告诉我一下,我觉得是st.length()//switch(st.substring(i, i+3)) {//case "001":buf.append("1");break;//case "010":buf.append("2");break;//case "011":buf.append("3");break;//case "100":buf.append("4");break;//case "101":buf.append("5");break;//case "110":buf.append("6");break;//case "111":buf.append("7");break;//default: break;//}String string = st.substring(i,i+3);if(string.equals("000")) buf.append("0");//一开始还不太理解为什么要这个,如果前面是三个零直接可以省略不要了,后来想想,有可能在中间出现的,例如111 111 000 101.else if(string.equals("001")) buf.append("1");else if(string.equals("010")) buf.append("2");else if(string.equals("011")) buf.append("3");else if(string.equals("100")) buf.append("4");else if(string.equals("101")) buf.append("5");else if(string.equals("110")) buf.append("6");else if(string.equals("111")) buf.append("7");}return buf.toString();}private static String toBinary(String string) {char[] ch = string.toCharArray();StringBuffer buf = new StringBuffer();for(int i=0;i<ch.length;i++) {switch(ch[i]) {case '0':buf.append("0000");break;case '1':buf.append("0001");break;case '2':buf.append("0010");break;case '3':buf.append("0011");break;case '4':buf.append("0100");break;case '5':buf.append("0101");break;case '6':buf.append("0110");break;case '7':buf.append("0111");break;case '8':buf.append("1000");break;case '9':buf.append("1001");break;case 'A':buf.append("1010");break;case 'B':buf.append("1011");break;case 'C':buf.append("1100");break;case 'D':buf.append("1101");break;case 'E':buf.append("1110");break;case 'F':buf.append("1111");break;default: break;}}return buf.toString();}}

这段代码就可以了,最开始我都是用的字符串相加,但是运行超时了,后来再仔细看看他们的代码,都用的StringBuffer来实现字符串拼接,这里我借鉴的别人的方法。介绍的挺不错的。
借鉴:https://blog.csdn.net/qq_35187119/article/details/81280100
https://blog.csdn.net/catchingsun/article/details/50571593

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。