根据正常的思路,大家都能想到先转换成二进制在转换成八进制,但是其中涉及到效率的问题,前前后后经过了十几次测试,大多数都是超时,经过优化的代码最终通过了测试,请记住StringI和Number的API很耗时,慎用
第一次代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().cal();
}
public void cal(){
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
String[] list = new String[len];
for (int i = 0; i < list.length; i++) {
list[i] = sc.next();
}
String[] arr = new String[len];
for (int i = 0; i < list.length; i++) {
String str = list[i];
StringBuilder result = new StringBuilder();
for (int j = 0; j < str.length(); j++) {
char ch = str.charAt(j);
if(ch == 'a' || ch == 'A'){
result.append("1010");
}else if(ch == 'b' || ch == 'B'){
result.append("1011");
}else if(ch == 'C' || ch == 'C'){
result.append("1100");
}else if(ch == 'd' || ch == 'D'){
result.append("1101");
}else if(ch == 'e' || ch == 'E'){
result.append("1110");
}else if(ch == 'f' || ch == 'F'){
result.append("1111");
}else if(ch == '1'){
result.append("0001");
}else if(ch == '2'){
result.append("0010");
}else if(ch == '3'){
result.append("0011");
}else if(ch == '4'){
result.append("0100");
}else if(ch == '5'){
result.append("0101");
}else if(ch == '6'){
result.append("0110");
}else if(ch == '7'){
result.append("0111");
}else if(ch == '8'){
result.append("1000");
}else if(ch == '9'){
result.append("1001");
}else if(ch == '0'){
result.append("0000");
}
}
if(result.length() % 3 != 0){
result.insert(0, "0");
}
if(result.length() % 3 != 0){
result.insert(0, "0");
}
arr[i] = "" + result;
}
BigInteger[] ll = new BigInteger[len];
for(int t = 0; t< arr.length ;t ++){
String str = arr[t];
StringBuffer buf = new StringBuffer();
for(int x = 0;x<str.length() - 2;x+=3){
int fir = Integer.parseInt(str.charAt(x) +"");
int sec = Integer.parseInt(str.charAt(x+1) +"");
int thr = Integer.parseInt(str.charAt(x+2) +"");
int r = fir *4 + sec * 2 + thr;
buf.append(r);
}
ll[t] = new BigInteger(buf.toString());
}
printArray(ll);
}
private void printArray(BigInteger[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
第n次代码(可通过测试):
import java.util.Scanner;
public class Main {
static StringBuilder[] octals = null;//保存八进制数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
octals = new StringBuilder[len];
for (int i = 0; i < len; i++) {
trans(sc.next(),i);
}
for (int i = 0; i < octals.length; i++) {
System.out.println(octals[i]);
}
}
public static void trans(String value,int i){
StringBuilder sb = new StringBuilder();
StringBuilder result = new StringBuilder();
char[] c = value.toCharArray();
for (int j = 0; j < value.length(); j++) {
switch(c[j]){
case '1' : result.append("0001");break;
case '2' : result.append("0010");break;
case '3' : result.append("0011");break;
case '4' : result.append("0100");break;
case '5' : result.append("0101");break;
case '6' : result.append("0110");break;
case '7' : result.append("0111");break;
case '8' : result.append("1000");break;
case '9' : result.append("1001");break;
case '0' : result.append("0000");break;
case 'A' : result.append("1010");break;
case 'B' : result.append("1011");break;
case 'C' : result.append("1100");break;
case 'D' : result.append("1101");break;
case 'E' : result.append("1110");break;
case 'F' : result.append("1111");break;
}
}
if(result.length() % 3 != 0){
result.insert(0, "0");
}
if(result.length() % 3 != 0){
result.insert(0, "0");
}
for (int j = 0; j < result.length() -2; j += 3) {
String s = result.substring(j,j+3);
if(s.equals("000")){
if(j !=0){
sb.append('0');//如果是第一位就不要放0
}
}
else if(s.equals("001"))
sb.append('1');
else if(s.equals("010"))
sb.append('2');
else if(s.equals("011"))
sb.append('3');
else if(s.equals("100"))
sb.append('4');
else if(s.equals("101"))
sb.append('5');
else if(s.equals("110"))
sb.append('6');
else if(s.equals("111"))
sb.append('7');
}
octals[i] = sb;
}
}
此次的代码没有使用官方的工具类
感觉用官方工具类的话跟外挂没区别
像第二步完全可以用以下方法来实现
1.System.out.printf("%o");
2.Integer.toOctalString();