백준
백준[11005]번 : 진법 변환 2 ( JAVA )
하루우울루
2023. 4. 8. 19:15
https://www.acmicpc.net/problem/11005
11005번: 진법 변환 2
10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를
www.acmicpc.net
Code
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashMap<Integer,Character> map = new HashMap<>();
StringBuilder sb = new StringBuilder();
int a = sc.nextInt(); // 10진수의 수 60466175 36
int n = sc.nextInt(); // n진법
if(n > 10) {
for(int i=0;i<10;i++) {
map.put(i,(char)(i+48));
}
for(int i=10;i<n;i++) {
map.put(i,(char)(i+55));
}
}
else {
for(int i=0;i<n;i++) {
map.put(i,(char)(i+48));
}
}
while(a!=0) {
int k = a%n;
sb.append(map.get(k));
a/=n;
}
sb.reverse();
System.out.println(sb);
sc.close();
}
}
이전 문제인 진법 변환과 마찬가지로 개념은 똑같다.
https://qkrehdwns032.tistory.com/31
백준[2745]번 : 진법 변환 ( JAVA )
https://www.acmicpc.net/problem/2745 2745번: 진법 변환 B진법 수 N이 주어진다. 이 수를 10진법으로 바꿔 출력하는 프로그램을 작성하시오. 10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다.
qkrehdwns032.tistory.com
코드를 보면 쉽게 이해할 수 있을 것이다.