백준 11005번: 진법 변환2 [구현] -Java :: 매운코딩
728x90
300x250

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

 

11005번: 진법 변환 2

10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를

www.acmicpc.net

어떠한 수 N을 입력받는 진법으로 변환하는 문제

0.5초의 시간제한이나 최악의 경우에도 시간복잡도는 O(logB N)이다.

 

Java 코드는 아래를 참고하세요

728x90
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		//System.setIn(new FileInputStream("Test.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());

		int N = Integer.parseInt(st.nextToken());
		int B = Integer.parseInt(st.nextToken());
		
		int NOW = N;
		String res = "";
		char[] dec = new char[36];
		char ch = 'A';
		
		for (int i = 10; i < dec.length; i++) {
			dec[i] = (char) (ch++);
		}
		
		while(true) {
			int mok = NOW / B;
			int nmg = NOW % B;
			if(nmg>=10) {
				res = dec[nmg]+res;
			} else {
				res = nmg+""+res;
			}
			
			NOW = mok;
//			System.out.println("mok= "+mok+",nmg= "+nmg+",res= "+res);
			if (NOW == 0)
				break;

		}
		
		System.out.println(res);
	}

}
728x90

+ Recent posts