【蓝桥杯】进制转换

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
//	static int[] weight= {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
//	static char[] ch= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//	static char[] ch= {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};
	static LinkedList<Integer> list=new LinkedList<Integer>();
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
//		Scanner sc=new Scanner(System.in);
//		BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
		InputReader sc=new InputReader(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=sc.nextInt();
		while(n-->0) {
			String str=sc.nextLine();
			StringBuffer s=new StringBuffer();
			for(int i=0;i<str.length();i++) {
				switch (str.charAt(i)) {
				case '0':
					s.append("0000");
					break;
				case '1':
					s.append("0001");
					break;
				case '2':
					s.append("0010");
					break;
				case '3':
					s.append("0011");
					break;
				case '4':
					s.append("0100");
					break;
				case '5':
					s.append("0101");
					break;
				case '6':
					s.append("0110");
					break;
				case '7':
					s.append("0111");
					break;
				case '8':
					s.append("1000");
					break;
				case '9':
					s.append("1001");
					break;
				case 'A':
					s.append("1010");
					break;
				case 'B':
					s.append("1011");
					break;
				case 'C':
					s.append("1100");
					break;
				case 'D':
					s.append("1101");
					break;
				case 'E':
					s.append("1110");
					break;
				case 'F':
					s.append("1111");
					break;
				default:
					break;
				}
			}
			StringBuffer t=new StringBuffer("0");
			while (s.length()%3!=0) {
				s=new StringBuffer("0"+s);
//				s=t.append(s);
			}
			StringBuffer ans=new StringBuffer();
			for(int i=0;i<s.length()/3;i++) {				
				switch (s.substring(i*3, i*3+3)) {
				case "000":
					ans=ans.append("0");
					break;
				case "001":
					ans=ans.append("1");
					break;
				case "010":
					ans=ans.append("2");
					break;
				case "011":
					ans=ans.append("3");
					break;
				case "100":
					ans=ans.append("4");
					break;
				case "101":
					ans=ans.append("5");
					break;
				case "110":
					ans=ans.append("6");
					break;
				case "111":
					ans=ans.append("7");
					break;

				default:
					break;
				}
			}
			int key=0;
			for(int i=0;i<ans.length();i++) {
				if(ans.charAt(i)=='0') {
					key++;
				}else {
					break;
				}
			}
			out.println(ans.substring(key));
		}
		
		
		out.flush();
		out.close();
		
	}
}


class InputReader{
	private final InputStream stream;
	private final byte[] buf=new byte[8192];
	private int curChar,snumChars;
	public InputReader(InputStream st) {
		this.stream=st;
	}
	public int read() {
		if(snumChars==-1)
			throw new InputMismatchException();
		if(curChar>=snumChars) {
			curChar=0;
			try {
				snumChars=stream.read(buf);
			}catch (IOException e) {
				// TODO: handle exception
				throw new InputMismatchException();
			}
			if(snumChars<=0)
				return -1;
		}
		return buf[curChar++];
	}
	public int nextInt() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		int sgn=1;
		if(c=='-') {
			sgn=-1;
			c=read();
		}
		int res=0;
		do {
			res*=10;
			res+=c-'0';
			c=read();
		}while(!isSpaceChar(c));
		return res*sgn;
	}
	public long nextLong() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		int sgn=1;
		if(c=='-') {
			sgn=-1;
			c=read();
		}
		int res=0;
		do {
			res*=10;
			res+=c-'0';
			c=read();
		}while(!isSpaceChar(c));
		return res*sgn;
	}
	public int[] nextIntArray(int n) {
		int a[]=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=nextInt();
		}
		return a;
	}
	public String readString() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isSpaceChar(c));
		return res.toString();
	}
	public String nextLine() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isEndOfLine(c));
		return res.toString();
	}
	private boolean isEndOfLine(int c) {
		// TODO Auto-generated method stub
		return c=='\n'||c=='\r'||c==-1;
	}
	private boolean isSpaceChar(int c) {
		// TODO Auto-generated method stub
		return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
	}
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值