[BaekJoon] #2745 - 진법 변환 [Java][C++]
[BaekJoon] #2745 - 진법 변환 [Java][C++]
1. 아이디어
$B$진법의 수 $N$을 10진법으로 바꿔 출력하는 문제로 진법 변환 내장 함수를 활용하면 간단하게 해결할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(D)$ | $O(D)$ |
($D$ = n의 자릿수)
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.*;
import java.util.*;
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());
String n = st.nextToken();
int b = Integer.parseInt(st.nextToken());
System.out.println(Integer.parseInt(n, b));
}
}
Integer.parseInt 메서드로 진법 변환을 할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string n;
int b;
cin >> n >> b;
cout << stoi(n, nullptr, b);
}
std::stoi 함수로 진법 변환을 할 수 있다.
This post is licensed under CC BY 4.0 by the author.