[백준] 1244. 스위치 켜고 끄기(실버4)

ERror.ASER·2021년 2월 26일
0

백준

목록 보기
21/69
post-thumbnail

백준(실버4) - 10867. 중복 빼고 정렬하기(실버5)



풀이

import java.io.IOException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt()+1;
		int[] switches = new int[n];
		
		for(int i=1; i<n; i++) {//0번쨰는 없다고 생각하기.
			switches[i] = sc.nextInt();
		}
		
		int sNum = sc.nextInt();//학생수
		
		
		for(int i=0; i<sNum; i++) {//학생 성별과 받은 수 넣기
			int s = sc.nextInt(); //성별
			int number = sc.nextInt();//받은 수
			
			if(s == 1) {//남자라면, 배수
				for(int j=number; j< n; j+=number) {
					switches[j] = switches[j] == 0? 1:0;
				}
			}else if(s==2) {//여자라면,받은 수의 대칭들 상태를 바꿔준다.
				int left = number-1;
				int right = number+1;
				while(true) {
					if(left<1 || right>=n) break;
					if(switches[left]!=switches[right]) break;
					left--;
					right++;
				}
				left++;
				right--;
				for(int k=left; k<=right; k++) {
					switches[k] = switches[k] == 0? 1:0;
				}
				
			}
			
		}
		
		for(int i=1; i<n; i++) {
			System.out.print(switches[i]+" ");
			if(i%20 ==0) System.out.println();
		}	
	}
}
profile
지우의 블로그

0개의 댓글