[백준/Python] 8958 - OX퀴즈

orangesnail·2025년 3월 5일

백준

목록 보기
51/169

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


초기 코드

t = int(input())

for _ in range(t):
    answer = input()
    score = count = 0

    for i in range(len(answer)):
        if answer[i] == "O":
            count += 1
            score += count
        
        if answer[i] == "X":
            count = 0

    print(score)

코드 개선하기

  1. 문자열을 입력받은 후 strip()를 사용해 앞뒤 공백을 제거해 안정성을 높인다.
  2. 인덱스를 사용하지 말고 문자열을 직접 순회한다! (c에서 하는 방식에 익숙해져 있어서 또 인덱스를 써버렸다...)
t = int(input())

for _ in range(t):
    answer = input().strip()
    score = count = 0

    for a in answer:
        if a == "O":
            count += 1
            score += count
        else:
            count = 0

    print(score)
profile
초보입니다. 피드백 환영합니다 😗

0개의 댓글