| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
													Tags
													
											
												
												- getline
- double ended queue
- 자료구조
- 매크로
- 2557
- iOS14
- 프레임워크와 라이브러리의 차이
- vscode
- 연결요소
- string 함수
- k-eta
- 시간복잡도
- 백준
- Django의 편의성
- EOF
- correlation coefficient
- UI한글변경
- 알고리즘 공부방법
- 이분그래프
- 엑셀
- 표준 입출력
- Django Nodejs 차이점
- 입/출력
- Django란
- 장고란
- scanf
- 입출력 패턴
- string 메소드
- 구조체와 클래스의 공통점 및 차이점
- c++
													Archives
													
											
												
												- Today
- Total
Storage Gonie
(6) [C++, Java] 백준 No.2745 : 진법 변환 본문
반응형
    
    
    
  문제

풀이
자세한 풀이 :
# C++(나의 풀이)
#include <iostream>
using namespace std;
int main() {
    string N;
    int B;
    cin >> N >> B;
    int result = 0; // 초기화 꼭해주기
    for (int i = 0; i < N.size(); i++)
    {
        int temp;
        // B진수의 각 자리값(문자)을 0 ~ 35의 숫자로 변환하기
        if (N[i] <= 57)            // '9' : 57
            temp = N[i] - 48;            
        else
            temp = N[i] - 55;       // 'A' : 65, 'Z' : 90
        // 각 자리의 수 * B^k 해주기
        for(int j = 0; j < N.size()-i-1 ; j++)
                temp *= B;
        // 각 자리에서 계산된 값 최종 출력값에 합해주기
        result += temp;
    }
    
    cout << result;
}# C++(백준 풀이)
- ans = 0;
   각 자리에 대해서 ans = (ans * A) + N[i] 적용
#include <iostream>
#include <string>
using namespace std;
int main() {
    int ans = 0;
    string s;
    int b;
    
    cin >> s >> b;
    
    for (int i=0; i<s.size(); i++) 
    {
        if ('0' <= s[i] && s[i] <= '9')
            ans = ans * b + (s[i] - '0');
        else 
            ans = ans * b + (s[i] - 'A' + 10);
    }
    
    cout << ans << '\n';
}# Java
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String[] line = sc.nextLine().split(" ");
        String s = line[0];
        int b = Integer.valueOf(line[1]);
        int ans = 0;
        for (int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if ('0' <= c && c <= '9') {
                ans = ans * b + (c - '0');
            } else {
                ans = ans * b + (c - 'A' + 10);
            }
        }
        System.out.println(ans);
    }
}
반응형
    
    
    
  '알고리즘 > 백준풀이7. 수학' 카테고리의 다른 글
| (8) [C++, Java] 백준 No.1212 : 8진수 2진수 (2) | 2019.05.07 | 
|---|---|
| (7) [C++, Java] 백준 No.1373 : 2진수 8진수 (0) | 2019.05.06 | 
| (5) [C++, Java] 백준 No.11005 : 진법 변환2 (0) | 2019.05.04 | 
| (4) [C++, Java] 백준 No.9613 : GCD 합 (0) | 2019.05.04 | 
| (3) [C++, Java] 백준 No.1934 : 최소공배수 (0) | 2019.05.04 | 
 
            
 
								 
								 
								