백준 알고리즘
[Bronze I] 진법 변환 2 - 11005
성능 요약
메모리: 13152 KB, 시간: 4 ms
분류
수학, 구현
문제 설명
10진법 수 N이 주어진다. 이 수를 B진법으로 바꿔 출력하는 프로그램을 작성하시오.
10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를 사용한다.
A: 10, B: 11, ..., F: 15, ..., Y: 34, Z: 35
입력
첫째 줄에 N과 B가 주어진다. (2 ≤ B ≤ 36) N은 10억보다 작거나 같은 자연수이다.
출력
첫째 줄에 10진법 수 N을 B진법으로 출력한다.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let mut iter = input.trim().split_whitespace();
let n: u32 = iter.next().unwrap().parse().unwrap();
let b: u32 = iter.next().unwrap().parse().unwrap();
let mut b_num = String::new();
let mut tmp = n;
while tmp != 0 {
let remainder = tmp % b;
if remainder > 9 {
let c = char::from_u32(remainder - 10 + b'A' as u32).unwrap();
b_num.push(c);
} else {
b_num.push(char::from_digit(remainder, 10).unwrap());
}
tmp /= b;
}
b_num = b_num.chars().rev().collect();
println!("{}", b_num);
}