[백준] 2745번 : 진법 변환 - JAVA

2024. 3. 31. 17:38·백준

2745 진법 변환

 

 

이번 문제는 임의의 진수와 해당 진수로 이루어져 있는 값을 입력받아 10진수로 변환하는 문제이다.

예를 들어 8진수 77은 이진수로 1 1 1 1 1 1이고(8진수는 2진수를 3자리씩 자름, 2진수로 8은 111) 10진수로 변경하면 63이다.

 

$8^1 * 7 + 8^0 * 7 = 63$

 

 

성공 소스

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());
        StringBuilder sb = new StringBuilder();

        String n = st.nextToken();
        int b = Integer.parseInt(st.nextToken());
        int result = 0;

        for (int i = 0; i < n.length(); i++) {
            // 지수
            int square = n.length() - i - 1;

            // 문자인 경우
            if (n.charAt(i) > 57) {
                result += (int) (Math.pow(b, square) * (n.charAt(i) - 55));
            } else {
                result += (int) (Math.pow(b, square) * (n.charAt(i) - '0'));
            }
        }

        System.out.println(result);
        br.close();
    }

}

 

저작자표시 비영리 변경금지 (새창열림)

'백준' 카테고리의 다른 글

[백준] 11576번 : Base Conversion - JAVA  (0) 2024.03.31
[백준] 11005번 : 진법 변환 2 - JAVA  (0) 2024.03.31
[백준] 11561번 : 징검다리 - JAVA  (0) 2024.03.31
[백준] 1966번 : 프린터 큐- JAVA  (0) 2024.03.28
[백준] 골드 달성 후기  (0) 2024.03.24
'백준' 카테고리의 다른 글
  • [백준] 11576번 : Base Conversion - JAVA
  • [백준] 11005번 : 진법 변환 2 - JAVA
  • [백준] 11561번 : 징검다리 - JAVA
  • [백준] 1966번 : 프린터 큐- JAVA
masjeong
masjeong
교양이란 화를 내지 않으면서도 자신의 신념을 잃지 않은 채 어떤 얘기라도 들을 수 있는 능력을 말한다 - 로버트 프로스트
  • masjeong
    나자바바라쿠배
    masjeong
  • 전체
    오늘
    어제
    • 전체보기 (28)
      • Spring Boot (3)
      • Spring Batch (1)
      • MSA (3)
      • Docker (1)
      • 백준 (16)
      • 자료구조 (3)
      • Kafka (0)
      • DB (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    ratelimiter
    18111
    cloud native application
    백준
    알고리즘
    이분탐색
    springboot
    11561
    백준3041
    큐
    15829
    Java
    spring batch
    Queue
    Cloud Native Architecture
    18110
    이진탐색
    ExecutionContext
    mariadb
    오블완
    gradle 오류
    진법변환
    티스토리챌린지
    SpringCloud
    spring cloud Gateway
    백준11723
    2449
    정렬
    executortype
    MSA
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
masjeong
[백준] 2745번 : 진법 변환 - JAVA
상단으로

티스토리툴바