-
[알고리즘-JAVA] 백준 알고리즘 11005번 - 진법 변환2알고리즘 2019. 8. 7. 16:21
접근 과정
1. 어떤 문제로 이해 했는가? 그리고 문제의 제약 조건은?어떤 10진법수를 임의의 B진법으로 바꾸는 문제.
2. 나의 방식대로 문제를 재정의 하자.10진법수를 B로 계속해서 나누어 진법변환을 하자.
3. 어떤 알고리즘과 자료구조를 사용할 것인가?진법 변환
4. 어떻게 계산할 것인가?
1) 입력BufferedReader, InputStreamReader를 이용
2) 시간 복잡도 계산
O(n/b)
5. 주의할 점은 무엇인가?나머지를 저장하는 배열의 크기를 결정하기 힘드므로,
그냥 임의의 변수에 저장시킨다.
6. 풀이 과정
나머지를 ans라는 변수에 계속해서 저장시키고, 나중에는 거꾸로 출력하면 된다.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer stk = new StringTokenizer(br.readLine()); int n = Integer.parseInt(stk.nextToken()); int b = Integer.parseInt(stk.nextToken()); String ans = ""; int rest; while (n > 0) { rest = n % b; if (rest >= 10) { ans += (char)(rest - 10 + (int)'A'); } else { ans += (char)(rest + (int)'0'); } n /= b; } for (int i = ans.length() - 1; i >= 0; i--) { System.out.print(ans.charAt(i)); } } }
'알고리즘' 카테고리의 다른 글
[알고리즘-JAVA] 백준 알고리즘 2751번 - 수 정렬하기 2 (0) 2019.08.07 [알고리즘-JAVA] 백준 알고리즘 2745번 - 진법 변환 (0) 2019.08.07 [알고리즘-JAVA] 백준 알고리즘 1934번 - 최소공배수 (0) 2019.08.07 [알고리즘-JAVA] 백준 알고리즘 2609번 - 최대공약수와 최소공배수 (0) 2019.08.06 [알고리즘-JAVA] 백준 알고리즘 10430번 - 나머지 (0) 2019.08.06