STUDY/Algorithm

[백준] 1244 스위치 켜고 끄기

sinawi95 2021. 2. 10. 16:02
728x90

www.acmicpc.net/problem/1244

 

1244번: 스위치 켜고 끄기

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

www.acmicpc.net

수학문제들은 왜 이상한 행동을 할까

스위치 켜고 끄기하면 등짝 스매시 한대 맞을각인데...

import sys
#input = sys.stdin.readline
sys.stdin=open(".idea/inputs/1244_input1.txt","r")

sw = int(input())
sw_list = list(map(int, input().split()))
number_students = int(input())

for _ in range(number_students):
    print(sw_list)
    s, idx = map(int,input().split())
    if s == 1: # man
        for sw_idx in range(idx, sw+1, idx):
            sw_list[sw_idx-1] = int(not sw_list[sw_idx-1])
        print("man:",sw_list)
    else:   #woman
        idx -= 1 # 인덱스에 접근을 쉽게하기위해
        cnt = 1
        sw_list[idx] = int(not sw_list[idx])  # 본인 인덱스는 무조건 바뀜
        while (0 <= idx - cnt) and (idx + cnt< sw): #
            print("sw_list[idx-cnt]:",sw_list[idx-cnt],"sw_list[idx+cnt]:",sw_list[idx+cnt])
            if sw_list[idx-cnt] == sw_list[idx+cnt]:
                sw_list[idx-cnt] = int(not sw_list[idx-cnt])
                sw_list[idx+cnt] = int(not sw_list[idx+cnt])
                cnt +=1
            else:
                break
        print("woman:",sw_list)

# 스위치의 상태를 1번 스위치에서 시작하여 마지막 스위치까지 한 줄에 20개씩 출력한다.
# 예를 들어 21번 스위치가 있다면 이 스위치의 상태는 둘째 줄 맨 앞에 출력한다.
# 켜진 스위치는 1, 꺼진 스위치는 0으로 표시하고, 스위치 상태 사이에 빈칸을 하나씩 둔다.
result = ''
for i in range(sw): #1~21
    result += str(sw_list[i])
    if i % 20 == 19:
        result += '\n'
    else:
        result += ' '
print(result)


또 되네

출력 개수가 한줄에 20개씩 나와야하는 것때문에 틀렸고, 인덱스 접근할때 범위를 잘못잡아서 틀렸다.

항상 겪는 문제여서 반례찾는게 더 힘든거같다.

여기에서 sw_list[idx] = int(not sw_list[idx]) 라는 식을 볼수있는데 1이면 0으로 바꾸고 0이면 1로 바꾸는 식이다.

C는 True = 1 False=0 으로 되기때문에  var = !var 이런식으로 많이 사용하는데 파이썬은 되는지 몰라서 비슷하게 사용했다.

 

'STUDY > Algorithm' 카테고리의 다른 글

[백준] 11653 소인수분해  (0) 2021.02.12
[백준] 11659 구간 합 구하기 4  (0) 2021.02.11
[백준] 2628 종이자르기  (0) 2021.02.10
[백준] 2578 빙고  (0) 2021.02.10
[백준] 2615 오목  (0) 2021.02.09