Post

[BaekJoon] 2745번 - 진법 변환 [Java][C++]

[BaekJoon] 2745번 - 진법 변환 [Java][C++]

문제 링크


1. 문제 풀이


$B$ 진법의 수 $N$ 을 10진법으로 바꿔 출력하는 문제로 진법 변환 내장 함수를 활용하면 간단하게 해결할 수 있다.


2. 코드


1. 풀이 [Java]

Integer.parseInt 메서드로 진법 변환을 할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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));
    }
}


2. 풀이 [C++]

stoi 함수로 진법 변환을 할 수 있다.

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(false);
    cin.tie(nullptr);

    string n;
    int b;
    cin >> n >> b;
    cout << stoi(n, nullptr, b);
}

This post is licensed under CC BY 4.0 by the author.