[BOJ][11576] Base Conversion
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/11576
2. 사용 알고리즘
수학
3. 풀이
A진법 수를 10진법 수로 변환 후 B진법으로 변환
4. 소스 코드
4-1. C++
https://github.com/dev-aiden/problem-solving/blob/main/boj/11576.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
int a, b; cin >> a >> b;
int temp = 0;
int m; for (cin >> m; m--;) {
int num; cin >> num;
temp *= a;
temp += num;
}
vector<int> v;
while (temp != 0) {
v.push_back(temp % b);
temp /= b;
}
reverse(v.begin(), v.end());
int len = v.size();
for (int i = 0; i < len; ++i) cout << v[i] << " ";
cout << "\n";
return 0;
}
4-2. JAVA
https://github.com/dev-aiden/problem-solving/blob/main/boj/11576.java
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
29
30
31
32
33
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken());
int temp = 0;
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
for(int i = 0; i < m; ++i) {
int num = Integer.parseInt(st.nextToken());
temp *= a;
temp += num;
}
Vector<Integer> v = new Vector<>();
while(temp != 0) {
v.add(temp % b);
temp /= b;
}
Collections.reverse(v);
int len = v.size();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < len; ++i) sb.append(v.get(i)).append(" ");
System.out.println(sb);
}
}
Leave a comment