ALGORITHM/프로그래머스 | 백준 | 삼성 | 카카오

[백준] 1244 스위치 켜고 끄기 java

SZCODE 2021. 3. 30. 23:34

www.acmicpc.net/problem/1244

 

1244번: 스위치 켜고 끄기

첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩

www.acmicpc.net

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt()+1;
		int[] sw = new int[n];
		
		for (int i = 1; i < n; i++) {
			sw[i] = sc.nextInt();
		}
		int student = sc.nextInt();
		
		for(int j = 1; j<= student; j++) {
			int gender = sc.nextInt();
			int key = sc.nextInt();
			
			//남자라면
			if(gender == 1) {
				//key의 배수로
				for(int i = key; i<n; i+=key) sw[i]^=1;//xor(다르면 1)
			}
			//여자라면
			else if(gender == 2){
				int l = key-1;
				int r = key+1;
				
				while(true) {//대칭 찾아서
					if(l<1 || r>= n) break;
					if(sw[l] != sw[r]) break;
					l--; r++;
				}
				l++; r--;
				
				while(l<=r) {
					sw[l] ^=1;//xor
					l++;
				}
			}
		}
		
		for (int i = 1; i < n; i++) {
			System.out.print(sw[i]+" ");
			if(i%20==0) System.out.println();
		}
	
	}
}