백준 8958 OX퀴즈

c++문제풀이

문제

“OOXXOXXOOO”와 같은 OX퀴즈의 결과가 있다. O는 문제를 맞은 것이고, X는 문제를 틀린 것이다. 문제를 맞은 경우 그 문제의 점수는 그 문제까지 연속된 O의 개수가 된다. 예를 들어, 10번 문제의 점수는 3이 된다. “OOXXOXXOOO”의 점수는 1+2+0+0+1+0+0+1+2+3 = 10점이다. OX퀴즈의 결과가 주어졌을 때, 점수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 길이가 0보다 크고 80보다 작은 문자열이 주어진다. 문자열은 O와 X만으로 이루어져 있다.

출력

각 테스트 케이스마다 점수를 출력한다.

예제 입력

5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX

예제 출력

10
9
7
55
30

for 조건문
either an expression which is contextually convertible to bool. This expression is evaluated before each iteration, and if it yields false, the loop is exited.
A declaration of a single variable with a brace-or-equals initializer. the initializer is evaluated before each iteration, and if the value of the declared variable converts to false, the loop is exited.

전위연산자는 할당 전에 연산을 처리
후위연산자는 할당 이후에 연산을 처리
The difference between the two lies in their return values. The prefix increment returns the value of a variable after it has been incremented. On the other hand, the more commonly used postfix increment returns the value of a variable before it has been incremented.

#include<cstdio>
int t;
char s[81];
int main() {
	scanf("%d", &t);
	while (t--) {
		int r = 0,c=0;
		scanf("%s", s);
		for (int i = 0; s[i]; ++i) 
		  s[i] == 'O' ? r += ++c : c = 0;
		printf("%d\n", r);
	}
	return 0;
}

RPC Message procedure

Remote Procedure Call - Stateful Continue reading

PubSub architecture

Published on August 10, 2023

RESTful architecture

Published on August 09, 2023