Baekjoon/IM Level

[백준] 12927 배수 스위치 (실버4)

hellosonic 2023. 3. 14. 11:33

문제요약

나의 코드 및 설명 (실패코드)

  • 예제 입력 3까지는 정상적으로 출력되나, 예제 입력 4에서 무한입력이 되어버린다..
l = ['N']
light = list(input()) # yyyy

light_list = l + light # n yyyy #2번 스위치는 2, 4 끔 j % i == 0:
def onoff(x):
    if light_list[x] == 'Y':
        light_list[x] = 'N'
    elif light_list[x] == 'N':
        light_list[x] = 'Y'

count = 0
while True:
    if 'Y' not in light_list:
        print(count)
        break
    for i in range(1, len(light_list)): # 1
        for j in range(i, len(light_list)): # 1 2 3 4
            if j % i == 0:
                onoff(j)
        count += 1
        if 'Y' not in light_list:
            break
        else:
            continue

다른 코드 및 설명

  • 스위치 번호와 인덱스 번호를 맞추기 위해 리스트 switch의 인덱스 0의 값을 <0>으로 한다.
  • if switch[i] == 'N' : 예를들어 i = 3 일때 스위치 3번째 값이 N이라면, 4로 넘어가서 Y인지 N인지 확인한다.
switch = [0] + list(input())
cnt = 0

for i in range(1, len(switch)):
    if switch == [0] + ['N'] * (len(switch)-1):
        break
    if switch[i] == 'N':
        continue
    for j in range(i, len(switch)):
        if j % i == 0:
            if switch[j] == 'Y':
                switch[j] = 'N'
            else:
                switch[j] = 'Y'
    cnt += 1
print(cnt)