티스토리 뷰

문제 개요

주어지는 입력값에 대해 상태를 변경한 후 최종 결과물을 출력하는 문제입니다.

문제 접근

스위치 개수가 100개고 학생수가 100명인데 제한시간이 2초 이므로 100명이 모두 100번 바꾼다고 해도 충분히 문제 해결이 가능합니다. 단순 구현으로 생각하고 접근하였습니다.

코드

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

public class Main {
    static int N;
    static Boolean[] status;
    static int students;
    static int[][] operations;

    private static void input() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(bf.readLine());
        status = Arrays.stream(bf.readLine().split(" ")).map(st -> st.equals("1")).toArray(Boolean[]::new);
        students = Integer.parseInt(bf.readLine());
        operations = new int[students][];
        for(int i=0;i<students;i++){
            operations[i] = Arrays.stream(bf.readLine().split(" "))
                .mapToInt(Integer::parseInt)
                .toArray();
        }
        bf.close();
    }

    private static void male(int index){
        for(int i=index-1; i<N;i+=index){
            status[i] = !status[i];
        }
    }

    private static void female(int index){
        int relativeIndex = index - 1;
        for(int i=0;i<N/2;i++){
            if(relativeIndex+i >= N || relativeIndex-i < 0){
                return;
            }
            if(status[relativeIndex-i]!=status[relativeIndex+i]){
                return;
            }
            status[relativeIndex-i] = status[relativeIndex+i] = !status[relativeIndex-i];
        }
    }

    private static void process() {
        for(int i=0;i<students;i++){
            if(operations[i][0] == 1){
                male(operations[i][1]);
            }
            if(operations[i][0] == 2){
                female(operations[i][1]);
            }
        }
        for(int i=0;i<N;i++){
            if(status[i]){
                System.out.print("1 ");
            }
            else{
                System.out.print("0 ");
            }
            if((i+1) % 20 == 0){
                System.out.println("");
            }
        }
    }

    public static void main(String[] args) throws IOException {
        input();
        process();
    }
}

문제

https://www.acmicpc.net/problem/1244

'문제풀이 > 백준' 카테고리의 다른 글

14501. 퇴사  (0) 2024.01.09
2668. 숫자고르기  (0) 2024.01.07
2659. 십자카드 문제  (0) 2024.01.03
2302. 극장 좌석  (0) 2024.01.01
9205. 맥주 마시면서 걸어가기  (1) 2023.12.31
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/12   »
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 31
글 보관함