문제 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AXRSXf_a9qsDFAXS

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

접근 방법

1. M의 이진수 표현의 마지막 N개의 비트가 모두 켜져 있다면, (M + 1)는 2^N의 배수이다.

2. 따라서 (M + 1)가 2^N으로 나누어 떨어진다면 "ON"을 출력하고, 그렇지 않다면 "OFF"를 출력한다.

 

풀이

// [SWEA] 10726. 이진수 표현

import java.util.*;
import java.io.*;

// public
class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        int T = Integer.parseInt(br.readLine());

        for (int tc = 1; tc <= T; tc++) {
            st = new StringTokenizer(br.readLine());
            int N = Integer.parseInt(st.nextToken());
            int M = Integer.parseInt(st.nextToken());
            
            sb.append("#").append(tc).append(" ").append((M + 1) % (1 << N) == 0 ? "ON" : "OFF").append("\n");
        }
        
        System.out.println(sb);
    }
}

'Algorithm' 카테고리의 다른 글

[BOJ] 4779. 칸토어 집합  (1) 2024.01.30
[SWEA] 1230. 암호문3  (1) 2024.01.29
[SWEA] 1288. 새로운 불면증 치료법  (0) 2024.01.29
[LeetCode] 2667. Create Hello World Function  (0) 2024.01.29
[LeetCode] 2666. Allow One Function Call  (0) 2024.01.26

+ Recent posts