백준 8진수, 10진수, 16진수 - 11816 [JAVA] - 23년 2월 9일

Denia·2023년 2월 8일
0

코딩테스트 준비

목록 보기
149/201

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static public void main(String[] args) throws IOException {
        BjSolution sol = new BjSolution();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        sol.solution(br.readLine());
    }
}

class BjSolution {
    public void solution(String inputVal) {
        int ans = 0;

        if (inputVal.length() == 1) {
            System.out.println(Integer.parseInt(inputVal));
            return;
        }

        if (inputVal.startsWith("0")) {
            if (inputVal.charAt(1) == 'x') {
                ans = Integer.parseInt(inputVal.substring(2), 16);
            } else {
                ans = Integer.parseInt(inputVal, 8);
            }
        } else {
            ans = Integer.parseInt(inputVal, 10);
        }

        System.out.println(ans);
    }
}
profile
HW -> FW -> Web

0개의 댓글