ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준] 1244번 : 스위치 켜고 끄기 - 자바(JAVA)
    알고리즘/백준 2022. 4. 19. 00:01
    728x90

     

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.StringTokenizer;
    
    public class Main_1244_스위치켜고끄기 {
    
    	static class Student {
    		int gender;
    		int switchNumber;
    
    		public Student(int gender, int switchNumber) {
    			super();
    			this.gender = gender;
    			this.switchNumber = switchNumber;
    		}
    
    		@Override
    		public String toString() {
    			return "Student [gender=" + gender + ", switchNumber=" + switchNumber + "]";
    		}
    
    	}
    
    	static final int MAN = 1;
    	static final int WOMAN = 2;
    	static int[] switchConditions;
    
    	public static void main(String[] args) throws NumberFormatException, IOException {
    
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		int switchCount = Integer.parseInt(br.readLine());
    
    		switchConditions = new int[switchCount];
    		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
    		for (int i = 0; i < switchCount; i++) {
    			switchConditions[i] = Integer.parseInt(st.nextToken());
    		}
    
    		int studentCount = Integer.parseInt(br.readLine());
    		Student[] students = new Student[studentCount];
    		for (int i = 0; i < studentCount; i++) {
    			st = new StringTokenizer(br.readLine(), " ");
    			int gender = Integer.parseInt(st.nextToken());
    			int switchNumber = Integer.parseInt(st.nextToken());
    			students[i] = new Student(gender, switchNumber);
    		}
    
    		// 메인 로직
    		for (int i = 0; i < studentCount; i++) {
    			int gender = students[i].gender;
    			int switchNumber = students[i].switchNumber;
    
    			switch (gender) {
    			case MAN:
    				manChangeSwitch(switchNumber);
    				break;
    
    			case WOMAN:
    				womanChangeSwitch(switchNumber);
    				break;
    			}
    
    		}
    		StringBuilder sb = new StringBuilder();
    		int count =0;
    		for(int i=0; i< switchConditions.length; i++) {
    			count++;	
    			sb.append(switchConditions[i] + " ");
    			if(count%20 ==0) {
    				sb.append("\n");
    			}
    		}
    		System.out.print(sb);
    
    	}
    
    	private static void manChangeSwitch(int switchNumber) {
    		for (int i = 0; i < switchConditions.length; i++) {
    			if ((i + 1) % switchNumber == 0) {
    				changeSwitch(i);
    			}
    		}
    	}
    
    	private static void womanChangeSwitch(int switchNumber) {
    
    		switchNumber--;
    		int k = 0;
    		changeSwitch(switchNumber);
    
    		while (true) {
    
    			k++;
    			int tempLeft = switchNumber - k;
    			int tempRight = switchNumber + k;
    
    			if (!isValid(tempRight, tempLeft)) {
    				break;
    			}
    			if (!isSymmetry(tempRight, tempLeft)) {
    				break;
    			}
    
    			changeSwitch(tempRight);
    			changeSwitch(tempLeft);
    
    		}
    	}
    
    	private static void changeSwitch(int i) {
    		switchConditions[i] = switchConditions[i] > 0 ? 0 : 1;
    	}
    
    	private static boolean isValid(int tempRight, int tempLeft) {
    		return (tempRight < switchConditions.length && tempLeft >= 0);
    	}
    
    	private static boolean isSymmetry(int tempRight, int tempLeft) {
    		return (switchConditions[tempLeft] == switchConditions[tempRight]);
    	}
    
    }

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

     

    1244번: 스위치 켜고 끄기

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

    www.acmicpc.net

     

    문제 해석

    1부터 연속적으로 번호가 붙어있는 스위치들이 있습니다.

    스위치는 켜져 있거나 꺼져있는 상태입니다. (1은 켜져 있음, 0은 꺼져있음을 나타냅니다.)

    학생 몇 명을 뽑아서, 학생들에게 1 이상이고 스위치 개수 이하인 자연수를 하나씩 나누어주었습니다.

     

    여학생/ 남학생 별로 숫자에 대해 하는 일이 다릅니다.

     

    남학생

    스위치 번호가 자기가 받은 수의 배수이면, 그 스위치의 상태를 바꿉니다.

     

    여학생

    자기가 받은 수와 같은 버호가 붙은 스위치를 중심으로 좌우가 대칭이면서 가장 많은 스위치를 포함하는 구간을 찾아 그 구간에 속한 스위치의 상태를 모두 바꿉니다.

    만약 처음부터 바꿀 수 없다면 자기 자신 가진 스위치의 번호의 상태만 바꿉니다.

     

    문제 풀이 전 설계

    남학생의 경우는 배수를 탐색하면 쉬울것같고

    여학생의 경우 배열의 바깥 범위를 벗어나지 않도록 주의해야 할 것 같습니다.

     

     

    문제 풀이하면서

    실버 3치고 많이 까다로운 문제 같았습니다.

    우선 출력 형식도 20개씩 잘라서 출력해야 했습니다.

    배열도 유효성과 대칭성을 검사해야 했습니다.

     

     

    코드

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.StringTokenizer;
    
    public class Main_1244_스위치켜고끄기 {
    
    	static class Student {
    		int gender;
    		int switchNumber;
    
    		public Student(int gender, int switchNumber) {
    			super();
    			this.gender = gender;
    			this.switchNumber = switchNumber;
    		}
    
    		@Override
    		public String toString() {
    			return "Student [gender=" + gender + ", switchNumber=" + switchNumber + "]";
    		}
    
    	}
    
    	static final int MAN = 1;
    	static final int WOMAN = 2;
    	static int[] switchConditions;
    
    	public static void main(String[] args) throws NumberFormatException, IOException {
    
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		int switchCount = Integer.parseInt(br.readLine());
    
    		switchConditions = new int[switchCount];
    		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
    		for (int i = 0; i < switchCount; i++) {
    			switchConditions[i] = Integer.parseInt(st.nextToken());
    		}
    
    		int studentCount = Integer.parseInt(br.readLine());
    		Student[] students = new Student[studentCount];
    		for (int i = 0; i < studentCount; i++) {
    			st = new StringTokenizer(br.readLine(), " ");
    			int gender = Integer.parseInt(st.nextToken());
    			int switchNumber = Integer.parseInt(st.nextToken());
    			students[i] = new Student(gender, switchNumber);
    		}
    
    		// 메인 로직
    		for (int i = 0; i < studentCount; i++) {
    			int gender = students[i].gender;
    			int switchNumber = students[i].switchNumber;
    
    			switch (gender) {
    			case MAN:
    				manChangeSwitch(switchNumber);
    				break;
    
    			case WOMAN:
    				womanChangeSwitch(switchNumber);
    				break;
    			}
    
    		}
    		StringBuilder sb = new StringBuilder();
    		int count =0;
    		for(int i=0; i< switchConditions.length; i++) {
    			count++;	
    			sb.append(switchConditions[i] + " ");
    			if(count%20 ==0) {
    				sb.append("\n");
    			}
    		}
    		System.out.print(sb);
    
    	}
    
    	private static void manChangeSwitch(int switchNumber) {
    		for (int i = 0; i < switchConditions.length; i++) {
    			if ((i + 1) % switchNumber == 0) {
    				changeSwitch(i);
    			}
    		}
    	}
    
    	private static void womanChangeSwitch(int switchNumber) {
    
    		switchNumber--;
    		int k = 0;
    		changeSwitch(switchNumber);
    
    		while (true) {
    
    			k++;
    			int tempLeft = switchNumber - k;
    			int tempRight = switchNumber + k;
    
    			if (!isValid(tempRight, tempLeft)) {
    				break;
    			}
    			if (!isSymmetry(tempRight, tempLeft)) {
    				break;
    			}
    
    			changeSwitch(tempRight);
    			changeSwitch(tempLeft);
    
    		}
    	}
    
    	private static void changeSwitch(int i) {
    		switchConditions[i] = switchConditions[i] > 0 ? 0 : 1;
    	}
    
    	private static boolean isValid(int tempRight, int tempLeft) {
    		return (tempRight < switchConditions.length && tempLeft >= 0);
    	}
    
    	private static boolean isSymmetry(int tempRight, int tempLeft) {
    		return (switchConditions[tempLeft] == switchConditions[tempRight]);
    	}
    
    }

     

    댓글

Designed by Tistory.