본문 바로가기
코딩테스트_백준풀이

BOJ 1244 스위치 켜고 끄기 JAVA

by wanna_dev 2024. 1. 29.

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

 

1244번: 스위치 켜고 끄기

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

www.acmicpc.net

 


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

public class 스위치켜고끄기 {
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		int switches[] = new int[N+1];
		StringTokenizer st =new StringTokenizer(br.readLine());
		for(int i=1; i<=N; i++) {
			switches[i] = Integer.parseInt(st.nextToken()); 
		}
		//System.out.println(Arrays.toString(switches));
		int studentNum = Integer.parseInt(br.readLine());
		for(int i=0; i<studentNum; i++) {
			st = new StringTokenizer(br.readLine());
			
			int student = Integer.parseInt(st.nextToken());
			int num = Integer.parseInt(st.nextToken());
			
			if(student==1) {
				for(int j=1; j<=N; j++) {
					if(j%(num)==0) {
						if(switches[j] == 0) {
							
							switches[j] = 1;
						}
						else {
							switches[j] = 0;
						}
						
					}
				}
			}
			//System.out.println(Arrays.toString(switches)); 
			else {
				int startIdx = num;
				int left = startIdx-1;
				int right = startIdx+1;
				
				
				
				while(left>=1 && right<=N) {
					
					if(switches[left]==switches[right]) {
						if(switches[left] == 0 ) {
							switches[left]=1;
							switches[right]=1;
						}
						else if(switches[left] ==1 ) {
							switches[left]=0;
							switches[right]=0;
						}
						
						left--;
						right++;
						
					}
						
					
					
				}
				if(switches[startIdx] ==0) {
					switches[startIdx]=1;
				}
				else {
					switches[startIdx]=0;
				}
				
			}
		
		}
		
		for(int i=1; i<=N; i++) {
			System.out.print(switches[i]+" ");
			if(i%20==0)System.out.println();
		}
	}
}

 

'코딩테스트_백준풀이' 카테고리의 다른 글

말이 되고픈 원숭이 #1600 JAVA G3  (0) 2024.03.01
BOJ 2493 탑 G5 Java  (1) 2024.02.05
배 #1092 c++ 풀이  (2) 2023.11.24
센서 #2212 c++ 풀이  (0) 2023.11.24
강의실 배정 #11000  (1) 2023.11.23