티스토리 뷰

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

class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] firstLine = in.readLine().split(" ");
final int aRadix = Integer.parseInt(firstLine[0]);
final int bRadix = Integer.parseInt(firstLine[1]);
int secondLine = Integer.parseInt(in.readLine());
String[] thirdLine = in.readLine().split(" ");
int radix10Number = 0;
//10진법 변환
int power = 0;
for(int j=secondLine-1; j>=0; --j) {
int element = Integer.parseInt(thirdLine[j]);
power = power == 0 ? 1 : power * aRadix;
radix10Number += (element * power);
}
//b진법 변환
Stack<Integer> stack = new Stack<>();
while(radix10Number != 0) {
stack.push(radix10Number % bRadix);
radix10Number /= bRadix;
}
while(!stack.isEmpty()) {
int size = stack.size();
if(size == 1) {
System.out.println(stack.pop());
} else {
System.out.print(stack.pop() + " ");
}
}
}
}


간단하게 10진법 변환 후 다른 진법으로 변환해주자.



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

let aRadix = firstLine.first!

let bRadix = firstLine.last!

let secondLine = Int(readLine()!)!

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


var radix10Number = 0

var power = 0

for i in stride(from: secondLine - 1, through: 0, by: -1) {

    let element = thirdLine[i]

    power = power == 0 ? 1 : power * aRadix

    radix10Number += (element * power)

}

var stack = [Int]()

while radix10Number != 0 {

    stack.append(radix10Number % bRadix)

    radix10Number /= bRadix

}

while !stack.isEmpty {

    print(stack.popLast()!, terminator: " ")

}

print()



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

6588번 '골드바흐의 추측'  (0) 2018.08.01
1929번 '소수 구하기'  (0) 2018.08.01
2089번 '-2진수'  (0) 2018.08.01
1212번 '8진수 2진수'  (0) 2018.07.31
1373번 '2진법 8진법'  (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
글 보관함