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

 

SW Expert Academy

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

swexpertacademy.com


풀이 과정

N과 M이 주어질 때, M의 이진수 표현의 마지막 비트 N자리가 모두 1인지를 판별하는 문제이다.

 

N자리가 모두 1로 차있는 이진수 T를 고려해보자. M과 T를 OR 연산을 시행했을때, M의 마지막 비트 N자리중에 0인 비트는 1로 바뀌게 되어 M은 다른 숫자로 변하게 된다. 따라서 N자리가 모두 1일때만 OR 연산을 시행 후에도 M의 값이 변하지 않게 됨을 이용해 비트마스킹으로 문제를 해결할 수 있다.


소스 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());

        for (int tc = 1; tc <= T; tc++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int N = Integer.parseInt(st.nextToken());
            int M = Integer.parseInt(st.nextToken());
            if ((M | ((1 << N)) - 1) == M) { // M과, 마지막 N비트에 1을 채운 수를 OR 연산한 값이 M이면, M의 마지막 N비트는 모두 1이다. 
                // 마지막 N비트에 1이 아닌 수가 있다면, OR 연산 이후에 1로 바뀌고, 기존의 M과 다른 수가 된다.
                System.out.printf("#%d %s\n", tc, "ON"); // 따라서 ON이다.
            } else {
                System.out.printf("#%d %s\n", tc, "OFF"); // 아니면 OFF다.
            }
        } // end of testcase
    } // end of main
} // end of class

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PobmqAPoDFAUq&categoryId=AV5PobmqAPoDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=2 

 

SW Expert Academy

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

swexpertacademy.com


풀이 과정

NxN 차원 배열에 시계방향으로 바깥쪽부터 안쪽까지 숫자를 1씩 늘려가면서 채우는 문제이다.

 

배열에 채워야 할 수가 NxN으로 정해져 있으므로 이미 채워진 블록, 배열의 끝을 만나면 방향을 진행 방향의 오른쪽으로 바꿔가며 배열에 수를 N^2번 채우면 된다.

 

처음 풀 때 while문을 이용해서 좀 지저분하게 짰는데 그 코드도 맨 아래에 남겨놓았다.


소스 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        // 동 남 서 북
        int[] dy = {0, 1, 0, -1};
        int[] dx = {1, 0, -1, 0};

        for (int tc = 1; tc <= T; tc++) {
            int N = Integer.parseInt(br.readLine());
            int[][] board = new int[N][N];
            int y = 0; int x = 0;
            int num = 1;
            int direction = 0;

            for (int i = 0; i < N*N; i++) {
                board[y][x] = num++;
                int ny = y + dy[direction];
                int nx = x + dx[direction];

                if (ny < 0 || ny > N-1 || nx < 0 || nx > N-1 || board[ny][nx] != 0) {
                    direction++;
                    if (direction == 4) {
                        direction = 0;
                    }
                }

                y += dy[direction];
                x += dx[direction];
            }

            System.out.printf("#%d\n", tc);

            for (int i = 0; i < N; i++) {
                for (int j : board[i]) {
                    System.out.print(j+" ");
                }
                System.out.println();
            }
        }
    }
}

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());

        for (int tc = 1; tc <= T; tc++) {
            int N = Integer.parseInt(br.readLine());
            int[][] board = new int[N][N];

            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    board[i][j] = 0;
                }
            }

            int direction = 0;
            int r = 0; int c = 0;
            int num = 1;
            while (board[r][c] == 0) {
                board[r][c] = num++;
                if (direction == 0 ) {
                    if (c+1 < N && board[r][c+1] == 0) {
                        c++;
                    } else {
                        if (r+1 < N && board[r+1][c] == 0) {
                            direction++;
                        } else {
                            break;
                        }
                    }
                }

                if (direction == 1) {
                    if (r+1 < N && board[r+1][c] == 0) {
                        r++;
                    } else {
                        if (c-1 >= 0 && board[r][c-1] == 0) {
                            direction++;
                        } else {
                            break;
                        }
                    }
                }

                if (direction == 2) {
                    if (c-1 >= 0 && board[r][c-1] == 0) {
                        c--;
                    } else {
                        if (r-1 >= 0 && board[r-1][c] == 0) {
                            direction++;
                        } else {
                            break;
                        }
                    }
                }

                if (direction == 3) {
                    if (r-1 >= 0 && board[r-1][c] == 0) {
                        r--;
                    } else {
                        if (c+1 < N && board[r][c+1] == 0) {
                            direction = 0;
                            c++;
                        } else {
                            break;
                        }
                    }
                }
            }

            System.out.printf("#%d\n", tc);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    System.out.print(board[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}

 

+ Recent posts