Resource constraints
Time limit: 1.0s Memory Limit: 512.0MB
Problem Description
Given a positive integer n hexadecimal, octal output thereof corresponds.
Input Format
The first act input a positive integer n (1 <= n <= 10). Next n lines, each line of a character string from 0 to 9, capital letters A ~ F composition indicates hexadecimal to convert a positive integer, each hexadecimal number length does not exceed 100,000.
Output Format
N output lines, each act input octal positive integer corresponding.
[Note] enter the hexadecimal number does not have leading 0, such as 012A. Octal number also can not have a leading zero.
Sample input
2 39 123ABC
Sample Output
71 4435274
【prompt】
First convert a hexadecimal number to a decimal number, and then converted by a binary number to octal.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
n = s.nextInt();
while (n > 0) {
n--;
String str;
char ansStr[] = new char[200010];
str = s.next();
int l = str.length();
int res = 0, k = 0, pos = 0;
for (int i = l - 1; i >= 0; i--) {
int temp = Integer.valueOf(str.charAt(i));
temp = temp>=65&&temp<=90 ? temp-65 + 10 : temp-Integer.valueOf('0') ;
int ttemp = 1;
if(k == 1)
ttemp = 2;
if(k == 2)
ttemp = 4;
res = ttemp * temp+ res;
ansStr[pos++] = (char)(res % 8+Integer.valueOf('0'));
k = (k + 1) % 3;
res = res