작성일 :

문제 링크

17389번 - 보너스 점수

설명

OX표를 앞에서부터 채점하며 기본 점수와 보너스 점수를 더해 총점을 구하는 문제입니다.


접근법

먼저 보너스 점수를 추적하는 변수를 두고 문자열을 앞에서부터 순서대로 확인합니다.

다음으로 각 문자가 O인 경우 현재 위치의 기본 점수에 보너스 점수를 더해 총점에 누적하고 보너스를 1 증가시킵니다. X인 경우 보너스 점수를 0으로 초기화합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var s = Console.ReadLine()!;

    var bonus = 0;
    var total = 0;
    for (var i = 0; i < n; i++) {
      if (s[i] == 'O') {
        total += (i + 1) + bonus;
        bonus++;
      } else bonus = 0;
    }

    Console.WriteLine(total);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; string s; cin >> n >> s;

  int bonus = 0;
  int total = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'O') {
      total += (i + 1) + bonus;
      bonus++;
    } else bonus = 0;
  }

  cout << total << "\n";

  return 0;
}