백준 11576번 Base Conversion JAVA

YB·2025년 3월 8일

링크텍스트

설명

문제가 이해되지않아 구글링해서 이해하고 풀었다. A진법의 수를 10진법으로 변환한 후 그 10진수를 B진법 수로 바꿔서 출력하는 문제이다.
시간복잡도: O(NLogN), 공간복잡도: O(NLogN)

코드

import java.util.*;
import java.io.*;

class Main {
	public static void main (String[] args) throws IOException {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    StringTokenizer st = new StringTokenizer(br.readLine());

		int a = Integer.parseInt(st.nextToken());
		int b = Integer.parseInt(st.nextToken());

		int m = Integer.parseInt(br.readLine());

		int decimal = 0;

		st = new StringTokenizer(br.readLine());
		for(int i=0;i<m;i++){
			decimal = decimal*a + Integer.parseInt(st.nextToken());
		}

		ArrayList<Integer> al = new ArrayList<>();

		while(decimal>0){
			al.add(decimal%b);
			decimal/=b;
		}
		
		for(int i=al.size()-1;i>=0;i--){
			System.out.print(al.get(i)+" ");
		}
	}
}

profile
안녕하세요

0개의 댓글