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

cheeeese·2022년 5월 14일
0

코딩테스트 연습

목록 보기
106/151
post-thumbnail

📖 문제

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

💻 내 코드

n=int(input())
switch=list(map(int, input().split()))
k=int(input())
for i in range(k):
    a, b=map(int, input().split())
    if a==1:
        for i in range(n):
            if (i+1)%b==0:
                if switch[i]==0:
                    switch[i]=1
                elif switch[i]==1:
                    switch[i]=0
            else:
                continue
            
    elif a==2:
        if switch[b-1]==0:
           switch[b-1]=1
        elif switch[b-1]==1:
            switch[b-1]=0
        
        i=1
        while b-1-i>=0 and b+i<=n:
            if switch[b-1-i]==switch[b-1+i]:
                if  switch[b-1-i]==0:
                    switch[b-1-i]=1
                    switch[b-1+i]=1
                elif switch[b-1-i]==1:
                    switch[b-1-i]=0
                    switch[b-1+i]=0
                
                i+=1
                
                
            else:
                break

for i in range(0, n, 20):
    print(*switch[i:i+20])

💡 풀이

  • 조건문만 잘 사용하면 되는 문제

가장 헤맸던 부분

while b-1-i>=0 and b+i<=n:
  • 처음에 or로 주었었음->하나라도 만족되면 계속 진행
  • 따라서 and가 맞음
  • 한 줄에 20개씩 출력하는 코드도 작성해주어야 한다
for i in range(0, n, 20):
    print(*switch[i:i+20])

0개의 댓글