Post

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

[백준] 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);
}

3. 풀이 정보

1. 구현 [Java]

언어시간메모리코드 길이
Java 11108 ms14364 KB417 B

2. 구현 [C++]

언어시간메모리코드 길이
C++ 170 ms2024 KB195 B

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