๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[EPPER/C++] ๋งž์•„๋ผ OX

by nitronium102 2021. 8. 31.

 

ํ’€์ด

'O'์ด ๋‚˜์˜ฌ ๋•Œ๋งˆ๋‹ค cnt๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ํ•ด๋‹น cnt๋ฅผ total์— ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

'X'๊ฐ€ ๋‚˜์™”์„ ๋•Œ ์ง€๊ธˆ๊นŒ์ง€ ๋‚˜์˜จ 'O'์˜ ๊ฐœ์ˆ˜๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋”ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 'O'๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๋งˆ๋‹ค ๋ˆ„์ ํ•ด์„œ ๋”ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค!

๋ฌธ์ œ๋ฅผ ์ž˜๋ชป ์ฝ์–ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค...

// ๋งž์•„๋ผ OX
#include <iostream>
#include <cstring>
using namespace std;

int solution(char input[]){
	int size = strlen(input);
	int total = 0, cnt = 0;
	
	for (int i=0; i<size; i++){
		if(input[i] == 'O'){
			cnt++;
			total += cnt;
		}
		else{
			cnt = 0;
		}
	}
	return total;
}

int main() {
	char input[100];
	cin >> input;
	int answer = solution(input);
	cout << answer;
	return 0;
}

๋Œ“๊ธ€