CS/BaekJoon

[BaekJoon] 백준 2745번 진법 변환 - Java

Bell91 2024. 4. 11. 14:58
반응형

🎈문제

https://www.acmicpc.net/problem/2745

 

💬설명

  • A~Z까지를 조건문으로 처리하도록 하자
  • charAt을 사용하여 한자리씩 처리하자

 

⌨️ CODE

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

public class Main {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int total = 0;
		int s = 1;
		StringTokenizer st = new StringTokenizer(br.readLine());
		String a = st.nextToken().trim();
		int b = Integer.parseInt(st.nextToken().trim());
		
		for(int i = a.length()-1 ; i >= 0 ; i--) {
			
			Character c = a.charAt(i);
			
			if(c >= 'A' && c <= 'Z') {
				total = total + (c - 'A' + 10) * s;
			}else {
				total = total + (c - '0') * s;
			}
			
			s = s*b;

		}
		
		System.out.println(total);
		br.close();
		
	}

}
반응형