基础练习 十六进制转八进制

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

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

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

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

样例输入
  2
  39
  123ABC

样例输出
  71
  4435274

  提示
  先将十六进制数转换成某进制数,再由某进制数转换成八进制。

怎么提交都是错误。最后发现是没考虑零的情况。以后要细心认真了。

 1 import java.io.IOException;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner input = new Scanner(System.in);
 7         String[] temp = new String[11];
 8         int n;
 9         StringBuffer[] b =new StringBuffer[11]; 
10         n = input.nextInt();
11         for(int i=0;i<n;i++){
12             temp[i] = input.next();
13         }
14         input.close();
15         //转十六进制
16         for(int i=0;i<n;i++){
17             StringBuffer temp1 = new StringBuffer();
18             for(int j=0;j<temp[i].length();j++){
19                 char a = temp[i].charAt(j);
20                 switch (a) {
21                 case '0':
22                     temp1.append("0000");//没有考虑的地方。
23                     break;
24                 case '1':
25                     temp1.append("0001");
26                     break;
27                 case '2':
28                     temp1.append("0010");
29                     break;
30                 case '3':
31                     temp1.append("0011");
32                     break;
33                 case '4':
34                     temp1.append("0100");
35                     break;
36                 case '5':
37                     temp1.append("0101");
38                     break;
39                 case '6':
40                     temp1.append("0110");
41                     break;
42                 case '7':
43                     temp1.append("0111");
44                     break;
45                 case '8':
46                     temp1.append("1000");
47                     break;
48                 case '9':
49                     temp1.append("1001");
50                     break;
51                 case 'A':
52                     temp1.append("1010");
53                     break;
54                 case 'B':
55                     temp1.append("1011");
56                     break;
57                 case 'C':
58                     temp1.append("1100");
59                     break;
60                 case 'D':
61                     temp1.append("1101");
62                     break;
63                 case 'E':
64                     temp1.append("1110");
65                     break;
66                 case 'F':
67                     temp1.append("1111");
68                     break;
69                 default:break;
70                 }
71                     
72             }
73             //翻转补零
74             temp1.reverse();
75             if(temp1.length()%3==1){
76                 temp1.append("00");
77             }
78             if(temp1.length()%3==2){
79                 temp1.append('0');
80             }
81             //转八进制
82             temp1.reverse();
83             int c;
84             for(int j=0;j<temp1.length();j++){
85                 c = (temp1.charAt(j)-'0')*4+(temp1.charAt(j+1)-'0')*2+(temp1.charAt(j+2)-'0');
86                 if(j==0&&c==0){
87                     j = j+2;
88                     continue;
89                 }
90                 System.out.print(c);
91                 j = j+2;
92             }
93             System.out.println();
94         }
95     }
96 }
原文地址:https://www.cnblogs.com/lolybj/p/6481673.html