https://www.acmicpc.net/problem/1244
문제
1부터 연속적으로 번호가 붙어있는 스위치들이 있다. 스위치는 켜져 있거나 꺼져있는 상태이다. <그림 1>에 스위치 8개의 상태가 표시되어 있다. ‘1’은 스위치가 켜져 있음을, ‘0’은 꺼져 있음을 나타낸다. 그리고 학생 몇 명을 뽑아서, 학생들에게 1 이상이고 스위치 개수 이하인 자연수를 하나씩 나누어주었다. 학생들은 자신의 성별과 받은 수에 따라 아래와 같은 방식으로 스위치를 조작하게 된다.
남학생은 스위치 번호가 자기가 받은 수의 배수이면, 그 스위치의 상태를 바꾼다. 즉, 스위치가 켜져 있으면 끄고, 꺼져 있으면 켠다. <그림 1>과 같은 상태에서 남학생이 3을 받았다면, 이 학생은 <그림 2>와 같이 3번, 6번 스위치의 상태를 바꾼다.
여학생은 자기가 받은 수와 같은 번호가 붙은 스위치를 중심으로 좌우가 대칭이면서 가장 많은 스위치를 포함하는 구간을 찾아서, 그 구간에 속한 스위치의 상태를 모두 바꾼다. 이때 구간에 속한 스위치 개수는 항상 홀수가 된다.
입력으로 스위치들의 처음 상태가 주어지고, 각 학생의 성별과 받은 수가 주어진다. 학생들은 입력되는 순서대로 자기의 성별과 받은 수에 따라 스위치의 상태를 바꾸었을 때, 스위치들의 마지막 상태를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩 있다. 셋째 줄에는 학생수가 주어진다. 학생수는 100 이하인 양의 정수이다. 넷째 줄부터 마지막 줄까지 한 줄에 한 학생의 성별, 학생이 받은 수가 주어진다. 남학생은 1로, 여학생은 2로 표시하고, 학생이 받은 수는 스위치 개수 이하인 양의 정수이다. 학생의 성별과 받은 수 사이에 빈칸이 하나씩 있다.
출력
스위치의 상태를 1번 스위치에서 시작하여 마지막 스위치까지 한 줄에 20개씩 출력한다. 예를 들어 21번 스위치가 있다면 이 스위치의 상태는 둘째 줄 맨 앞에 출력한다. 켜진 스위치는 1, 꺼진 스위치는 0으로 표시하고, 스위치 상태 사이에 빈칸을 하나씩 둔다.
간단한 문제 같은데 조건 처리 실수를 해서 여러 번 시도한 문제였다.
스위치를 배열로 입력받고, 성별에 따라 배열 원소를 바꾸면 되는 문제이다.
또한 배열의 index는 1부터 사용하였다.
성별과 숫자를 받아 switch문으로 처리해 주었다.
남성일 경우 배열의 끝까지 num의 배수만 숫자를 바꿨다.
여성은 left와 right가 배열의 index를 벗어나지 않고, 두 값이 같을 경우에만 idx를 늘려가면서 값을 바꿔주었다.
for (int i = 0; i < student; i++) {
int gender = sc.nextInt();
int num = sc.nextInt();
switch (gender) {
case 1: // 남자
for (int j = num; j <= N; j += num)
change(j); // 배수에 해당하는 index switch
break;
case 2: // 여자
int idx = 0;
change(num);
while (true) {
int right = num + idx;
int left = num - idx;
// right,left가 배열의 index 안이고 두 값이 대칭이면
if (right <= N && left > 0 && arr[right] == arr[left]) {
change(right);
change(left);
idx++;
} else {
break;
}
}
break;
}
}
출력은 20개씩 한 줄에 출력해야 하기 때문에, i가 20일 경우에는 newline을 추가하여 출력하였다.
StringBuilder sb = new StringBuilder();
for (int i = 1; i < arr.length; i++) {
sb.append(arr[i] + " ");
if (i % 20 == 0) {
sb.append("\n");
}
}
System.out.println(sb.toString());
전체 코드
package BOJ0809;
import java.util.Scanner;
public class BOJ_1224_스위치켜고끄기 {
static int[] arr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
arr = new int[N + 1]; // 스위치 배열, 0 은 사용x
for (int i = 1; i <= N; i++)
arr[i] = sc.nextInt();
int student = sc.nextInt();
for (int i = 0; i < student; i++) {
int gender = sc.nextInt();
int num = sc.nextInt();
switch (gender) {
case 1: // 남자
for (int j = num; j <= N; j += num)
change(j); // 배수에 해당하는 index switch
break;
case 2: // 여자
int idx = 0;
change(num);
while (true) {
int right = num + idx;
int left = num - idx;
// right,left가 배열의 index 안이고 두 값이 대칭이면
if (right <= N && left > 0 && arr[right] == arr[left]) {
change(right);
change(left);
idx++;
} else {
break;
}
}
break;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i < arr.length; i++) {
sb.append(arr[i] + " ");
if (i % 20 == 0) {
sb.append("\n");
}
}
System.out.println(sb.toString());
}
static void change(int i) {
if (arr[i] == 0) {
arr[i] = 1;
} else {
arr[i] = 0;
}
}
}
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 2407 - 조합 (자바/Java) BigInteger 클래스 (0) | 2022.08.09 |
---|---|
[백준] 2564 - 경비원 (자바/Java) (0) | 2022.08.09 |
[백준] 3273 - 두 수의 합 (자바/Java) (0) | 2022.08.07 |
[백준] 6588 - 골드바흐의 추측 (자바/Java) (0) | 2022.08.05 |
[백준] 15650 - N과 M (2) (자바/Java) 백트래킹 DFS 조합 (0) | 2022.08.03 |