Development Palette
n1244스위치켜고끄기 본문
1244번: 스위치 켜고 끄기
첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩
www.acmicpc.net
package com.hw.n1244스위치켜고끄기;
import java.util.Scanner;
public class Main3 {
public static int wm(int tmp) {
if (tmp == 0)
return 1;
else
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int status[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
status[i] = sc.nextInt();
}
int tmp = 0;
tmp = sc.nextInt();
int students = tmp;
int mw[] = new int[students];
int select[] = new int[students];
for (int i = 0; i < students; i++) {
mw[i] = sc.nextInt();
select[i] = sc.nextInt();
if (mw[i] == 1) // 남자일 때
{
int j = 1;
while (n >= select[i] * j) {
if (status[select[i] * j] == 1)
status[select[i] * j] = 0;
else
status[select[i] * j] = 1;
j++;
}
} else if (mw[i] == 2) // 여자일때
{
int w = 1;
boolean flag = false;
int before = 0;
int after = 0;
while (select[i] - w > 0 && select[i] + w <= n) {
if (status[select[i]-w] == status[select[i]+w]) {
flag = true;
before = select[i] - w;// 2 1
after = select[i] + w;// 4 5
w++;
}else
break;
}
if (flag == true) {
for (int j = before; j <= after; j++)
status[j] = wm(status[j]);
} else {
status[select[i]] = wm(status[select[i]]);
}
}
}
StringBuilder sb = new StringBuilder();
for (int b = 1; b < status.length; b++) {
if (b % 20 == 0)
sb.append(status[b] + "\n");
else
sb.append(status[b] + " ");
}
System.out.println(sb);
sc.close();
}
}
처음엔 아래 처럼 before, after를 if문 밖에서 먼저 할당을 했었다.
flag에서 true 일 때만 값을 넣어준다고 생각해서 상관 없을 거라고 생각했지만,,, 그렇게 하면 테스트케이스와 반례는 모두 맞지만 Fail이 뜬다.
조건문 안에 값을 할당하고 변수의 데이터 값이 잘못 되지? 않게 할당된 값만 사용 할 수 있게끔 조건문 내에 변수를 할당 해주자!!
오류 찾기 정말 정말 힘들었음
다음에도 이러한 케이스가 생길 수 있으니 변수할당의 위치를 주의하자!
while(select[i]-w >0 && select[i]+w <= n) {
before = select[i]-w;
after = select[i]+w;
if(status[before] == status[after]){
flag = true;
w++;
}else
break;
}
if (flag == true) {
for (int j = before; j <= after; j++){
status[j] = wm(status[j]);
}
}
else {
status[select[i]] = wm(status[select[i]]);
}
'Algorithm > Baekjoon' 카테고리의 다른 글
n2563_색종이 (0) | 2021.08.10 |
---|---|
[완전탐색] 조합식 문제 (0) | 2021.08.05 |
[스택] 후위 표기식 (0) | 2021.08.05 |
[재귀] 11729 하노이 탑 이동 순서 (0) | 2021.08.05 |
n2493_탑 (0) | 2021.08.05 |
Comments