• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • LeetCode
    • Codetree
    • SWEA
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[Codetree] 이상한 진수 2

17 Sep 2026

Reading time ~1 minute

  • 풀이 구현
해당 문제는 코드트리 이상한 진수 2에서 풀어보실 수 있습니다.

풀이 구현

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        // Please write your code here.
        int n = a.length();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = a.charAt(i)-'0';
        }        

        int max = Integer.MIN_VALUE;
        for(int i = 0; i < n; i++) {
            arr[i] = reverse(arr[i]); // 한자리만 반전
            max = Math.max(max,getDecimal(arr)); // 최대값
            arr[i] = reverse(arr[i]); // 해당자리 원상복구
        }        
        System.out.println(max);
    }
    public static int reverse(int num) {        
        if(num == 0) return 1;
        return 0;
    }

    public static int getDecimal(int[] arr) {
        int digit = 1, sum = 0;
        for(int i = arr.length - 1; i >= 0; i--) {
            sum+= digit*arr[i];
            digit = digit*2;
        }
        return sum;
    }
}

처음에 실수로 Math.max(max,getDecimal(arr));로 작성해서 최대값이 갱신이 안되는 문제가 있었다. max =을 앞에다 붙여주어야 갱신이 되는 점에 유의하자.



완전탐색 Share