티스토리 뷰

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] input = in.readLine().split(" ");
long n = Long.parseLong(input[0]);
int b = Integer.parseInt(input[1]);
solve(n, b);
System.out.println();
}
static void solve(long n, int b) {
if(n == 0) {
return;
} else {
solve(n / b, b);
long temp = n % (long)b;
//아스키코드 65번 A
if(temp >= 10) {
long transition = temp + 55;
System.out.print((char)transition);
} else {
System.out.print(temp);
}
}
}
}



재귀함수를 이용해 진법 변환하는 함수를 구현하였고,


아스키코드를 이용해 10 이상의 숫자를 알파벳으로 변환하였다.


n에 10억도 들어올 수 있으므로 long형으로 선언해주었다.....하지만 int형의 범위는 대략 21억까지라고 한다 int형으로 해도 괜찮겠다..



func solve(_ n: Int, _ b: Int) {

    if n == 0 {

        return

    }

    solve(n / b, b)

    let temp = n % b

    if temp >= 10 {

        let transition = Character(UnicodeScalar(temp + 55)!)

        print(transition, terminator: "")

    } else {

        print(temp, terminator: "")

    }

}


let input = readLine()!.split(separator: " ").map { Int($0)! }

solve(input.first!, input.last!)

print()


물론 Swift를 사용하여 코드의 양을 반 이상 줄이면서 시간도 훨씬 덜 걸리게 할 수 있다...


Swift에서는 아스키코드로 변환할 때 Character(UnicodeScalar(?)) 를 사용한다.

'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

1373번 '2진법 8진법'  (0) 2018.07.31
2745번 '진법 변환'  (0) 2018.07.31
9613번 'GCD 합'  (0) 2018.07.31
1934번 '최소공배수'  (0) 2018.07.31
2609번 '최대공약수와 최소공배수'  (0) 2018.07.31
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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 26 27 28
글 보관함