scone-lemon

[IM 대비] BOJ_1244 스위치 켜고 끄기 본문

ALGORITHM/BOJ

[IM 대비] BOJ_1244 스위치 켜고 끄기

lemon-scone 2021. 8. 29. 17:34

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

 

1244번: 스위치 켜고 끄기

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

www.acmicpc.net

 

 

우선 잘 돌아가지조차 않는.. 내 코드..

 

package IM_0829;

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

// 스위치 켜고 끄기
public class BOJ_1244 {
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		
		// 스위치 갯수, 스위치 배열 입력
		int N = Integer.parseInt(br.readLine()); // 스위치의 갯수
		int[] condition = new int[N+1];
		st = new StringTokenizer(br.readLine(), " ");
		for (int n = 01; n <= N; n++) {
			condition[n] = Integer.parseInt(st.nextToken());
		}
		System.out.println(Arrays.toString(condition));
		
		// 학생 수, 학생 배열 입력
		int S = Integer.parseInt(br.readLine()); // 학생 수
		int[][] student = new int[S][2];
		for (int i = 0; i < S; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			student[i][0] = Integer.parseInt(st.nextToken());
			student[i][1] = Integer.parseInt(st.nextToken());
		}
		System.out.println(Arrays.deepToString(student));
		
		// 스위치 조작 시작
		for (int i = 0; i < S; i++) {
			
			// 여학생이면
			if (student[i][0]==1) {
				
			}
			
			// 남학생이면
			else if (student[i][0]==2) {
				int num = 1;
				int check = 0;
				while (check < condition.length) {
					check = student[i][1] * num;
					System.out.println(check);
					condition[check] = condition[check] -1;
					System.out.println(Arrays.toString(condition));
				}
			}
		}
		
		for (int i = 0; i < condition.length; i++) {
			System.out.print(condition[i] + " ");
		}
		
	}
}

 

다음으로, 잘 돌아가는 솔루션 코드! 하지만 백준에서는 틀렸다고 떴다! 아왜!

 

package IM_0829;

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

// 스위치 켜고 끄기
public class BOJ_1244_sol {
	public static void main(String[] args) throws NumberFormatException, IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;

		// 스위치 갯수, 스위치 배열 입력
		int N = Integer.parseInt(br.readLine()); // 스위치의 갯수
		int[] condition = new int[N];
		st = new StringTokenizer(br.readLine(), " ");
		for (int n = 0; n < N; n++) {
			condition[n] = Integer.parseInt(st.nextToken());
		}
		//System.out.println(Arrays.toString(condition));

		// 학생 수, 학생 성별, 학생 부여숫자 입력
		int S = Integer.parseInt(br.readLine()); // 학생 수
		for (int i = 0; i < S; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			int gender = Integer.parseInt(st.nextToken());
			int number = Integer.parseInt(st.nextToken());
			
			// 남학생이면 (number 의 배수 위치 스위치 상태 변경)
			if (gender == 1) {
				for (int j = 0; j < condition.length; j++) {
					if ((j+1)%number==0) {
						if (condition[j]==0) {
							condition[j] = 1 - condition[j];
						}
					}
				}
			}
			
			// 여학생이면 (number를 중심으로 좌우대칭일 때 스위치 상태 변경)
			if (gender == 2) {
				int left = number - 1;
				int right = number - 1;
				while (left>=0 && right<condition.length && condition[left]==condition[right]) {
					condition[left] = 1 - condition[left];
					if (left != right) {
						condition[right] = 1 - condition[right];
					}
					left = left -1;
					right = right +1;
				}
			}
		}
		
		// 출력 (스위치를 한 줄에 20개씩 출력)
		for (int i = 0; i < condition.length; i++) {
			System.out.print(condition[i] + " ");
			if ((i + 1) % 20 == 0) {
				System.out.println();
			}
		}

	}
}

 

 

아래가 찐 솔루션

 

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

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int total = Integer.parseInt(br.readLine()); //스위치의 개수
		int[] switches = new int[total];
		StringTokenizer st = new StringTokenizer(br.readLine()); //스위치 string 받음
		for(int i=0; i<total; i++)
			switches[i] = Integer.parseInt(st.nextToken());
		
		int stuCnt = Integer.parseInt(br.readLine()); //학생의 명수
		for(int i=0; i<stuCnt; i++) {
			st = new StringTokenizer(br.readLine());
			int gender = Integer.parseInt(st.nextToken());
			int number = Integer.parseInt(st.nextToken());
			
			//남학생이면
			if(gender == 1) {
				for(int j=0; j<total; j++) //뽑은 수의 배수 위치에 있는 스위치의 상태를 바꾼다.
					if((j+1) % number == 0)
						switches[j] = switches[j] == 0? 1: 0;
			}
			//여학생이면
			else {
				//뽑은 수를 중심으로 좌우가 대칭이면 상태를 바꾼다.
				switches[number - 1] = switches[number - 1] == 0 ? 1 : 0;
				for(int j=1; j<total/2; j++) {
					if(number - 1 + j >= total || number - 1 - j < 0)
						break;
					if(switches[number - 1 - j] == switches[number - 1 + j]) {
						switches[number - 1 - j] = switches[number - 1 - j] == 0 ? 1 : 0;
						switches[number - 1 + j] = switches[number - 1 + j] == 0 ? 1 : 0;
					}
					else break; //대칭 아닌것이 나오면 바로 끝낸다.
				}
			}
		}
		
		//한 줄에 20개씩 출력
		for(int i=0; i<total; i++) {
			System.out.print(switches[i] + " ");
			if((i+1) % 20 == 0)
				System.out.println();
		}
	}
}