import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int n=input.nextInt(); input.nextLine(); for(int i=0;i<n;i++) { String str1 = input.nextLine(); String str2=""; //用来进行字符串的拼接 char a[] = str1.toCharArray(); //记录刚录入进去的字符串,变成字符数组 for(int j=0;j<str1.length();j++) //十六进制转换为二进制 { switch (a[j]) { case '0':str2+="0000";break; case '1':str2+="0001";break; case '2':str2+="0010";break; case '3':str2+="0011";break; case '4':str2+="0100";break; case '5':str2+="0101";break; case '6':str2+="0110";break; case '7':str2+="0111";break; case '8':str2+="1000";break; case '9':str2+="1001";break; case 'A':str2+="1010";break; case 'B':str2+="1011";break; case 'C':str2+="1100";break; case 'D':str2+="1101";break; case 'E':str2+="1110";break; case 'F':str2+="1111";break; default: break; } } // System.out.println(str2); 测试修正前的位数情况 //修正位数 int arry[] = new int[10000001]; if(str2.length()%3==1) { str2="00"+str2; } if(str2.length()%3==2) { str2="0"+str2; } // System.out.println(str2); 测试修正后的位数情况 // System.out.println(str2.length()); 测试修正后的位数是多少! char b[] = str2.toCharArray(); //修正位数之后原来的a字符数组当然不能使用了!,换成b字符数组 int j=0; //记录8进制的位数 for(int k=0;k<=str2.length()-3;k+=3) { arry[j]=(b[k]-'0')*4+(b[k+1]-'0')*2+(b[k+2]-'0'); j++; } for(int k=0;k<j;++k) { if(k==0&&arry[k]==0) continue; System.out.print(arry[k]); } System.out.println(); } } }
#include <iostream> #include <string> using namespace std; int arr[10000001]; int main() { int n,len_str,i,j; string str,str2; cin>>n; while(n--) { cin>>str; len_str=str.length(); str2=""; // 十六进制转换为二进制 for(i=0;i<len_str;++i) { switch(str[i]) { case '0':str2+="0000";break; case '1':str2+="0001";break; case '2':str2+="0010";break; case '3':str2+="0011";break; case '4':str2+="0100";break; case '5':str2+="0101";break; case '6':str2+="0110";break; case '7':str2+="0111";break; case '8':str2+="1000";break; case '9':str2+="1001";break; case 'A':str2+="1010";break; case 'B':str2+="1011";break; case 'C':str2+="1100";break; case 'D':str2+="1101";break; case 'E':str2+="1110";break; case 'F':str2+="1111";break; default:break; } } // 修正位数 if(len_str%3==1) str2="00"+str2; else if(len_str%3==2) str2="0"+str2; len_str=str2.length(); // 二进制转换八进制 j=0; for(i=0;i<=len_str-2;i+=3) { arr[j]=(str2[i]-'0')*4+(str2[i+1]-'0')*2+(str2[i+2]-'0'); ++j; } for(i=0;i<j;++i) { if(i==0 && arr[i]==0) continue; cout<<arr[i]; } cout<<endl; } return 0; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String a[] = new String[n]; for (int i = 0; i < n; i++) { a[i] = in.readLine(); } for (int i = 0; i < n; i++) { char[] temp = a[i].toCharArray(); StringBuffer s2 = new StringBuffer(); // 16 to 2 int k = temp.length; for (int j = 0; j < k; j++) { switch (temp[j]) { case '0': s2.append("0000"); break; case '1': s2.append("0001"); break; case '2': s2.append("0010"); break; case '3': s2.append("0011"); break; case '4': s2.append("0100"); break; case '5': s2.append("0101"); break; case '6': s2.append("0110"); break; case '7': s2.append("0111"); break; case '8': s2.append("1000"); break; case '9': s2.append("1001"); break; case 'A': s2.append("1010"); break; case 'B': s2.append("1011"); break; case 'C': s2.append("1100"); break; case 'D': s2.append("1101"); break; case 'E': s2.append("1110"); break; case 'F': s2.append("1111"); break; } } // 2 to 8 StringBuffer s3 = new StringBuffer(); int m = 0; if (4 * k % 3 == 1) { s3.append(s2.substring(0, 1)); m += 1; } else if (4 * k % 3 == 2) { switch (s2.substring(0, 2)) { case "01": s3.append("1"); break; case "10": s3.append("2"); break; case "11": s3.append("3"); break; default: break; } m += 2; } for (int j = m; j < 4 * k;) { switch (s2.substring(j, j + 3)) { case "000": s3.append("0"); break; case "001": s3.append("1"); break; case "010": s3.append("2"); break; case "011": s3.append("3"); break; case "100": s3.append("4"); break; case "101": s3.append("5"); break; case "110": s3.append("6"); break; case "111": s3.append("7"); break; } j += 3; } // delete 0 // use delete(old is 0) or charAt if (s3.length() == 2 && s3.charAt(0) == '0') {// 0-->00-->delete 00-->notany System.out.println(s3.substring(1)); } else { int q = 0; while (s3.charAt(q) == '0') { q++; } String s = s3.toString(); System.out.println(s3.substring(q)); } } } }
原文地址:http://blog.csdn.net/qq_16542775/article/details/44903199