성능 요약
메모리: 31256 KB, 시간: 52 ms
분류
구현(implementation), 문자열(string)
문제 설명
"OOXXOXXOOO"와 같은 OX퀴즈의 결과가 있다. O는 문제를 맞은 것이고, X는 문제를 틀린 것이다. 문제를 맞은 경우 그 문제의 점수는 그 문제까지 연속된 O의 개수가 된다. 예를 들어, 10번 문제의 점수는 3이 된다.
"OOXXOXXOOO"의 점수는 1+2+0+0+1+0+0+1+2+3 = 10점이다.
OX퀴즈의 결과가 주어졌을 때, 점수를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 길이가 0보다 크고 80보다 작은 문자열이 주어진다. 문자열은 O와 X만으로 이루어져 있다.
출력
각 테스트 케이스마다 점수를 출력한다.
내 풀이
a = int(input())
for i in range(a):
b = list(input())
c = 1
sum = 0
for i in b:
if i == 'O':
sum += c
c += 1
else:
c = 1
print(sum)
얻어갈 점
b = list(input())으로 b에 OOXXOXXOOO를 받으면, 한 개씩 잘개 쪼개져 print(b) -> ['O', 'O', 'X', 'X', 'O', 'X', 'X', 'O', 'O', 'O']이다.
이를 for i in b를 이용하여 사용할 수 있다.
'Algorithm > BOJ' 카테고리의 다른 글
[Bronze II] 상수 - 2908 // Python (0) | 2023.02.25 |
---|---|
[Bronze V] X보다 작은 수 - 10871 // Python (0) | 2023.02.25 |
[Bronze II] 음계 - 2920 // Python (0) | 2023.02.25 |
[Bronze III] 곱셈 - 2588 (0) | 2023.02.17 |