티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in BOJ


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Code

ㅇ Python

1
2
3
4
5
6
7
8
9
10
11
12
13
= int(input())
ox = input()
score = 0
bonus = 0
 
for i, res in zip(range(len(ox)), ox):
    if(res == 'O'):
        score += (i + 1+ bonus
        bonus += 1
    else:
        bonus = 0
 
print(score)
cs


ㅇ C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdio>
 
int main(void)
{
    int N, i, score = 0, bonus = 0;
    char ox[10001];
 
    scanf("%d"&N);
    scanf("%s"&ox);
    
    for (i = 0; i < N; i++)
    {
        if (ox[i] == 'O')
        {
            score += i + 1 + bonus;
            bonus++;
        }
        else
            bonus = 0;
    }
 
    printf("%d\n", score);
 
    return 0;
}
cs

#. Other Code

1
2
3
4
5
6
7
8
9
10
11
N, S = input(), input()
 
score, bonus = 00
 
for idx, res in enumerate(S):
    if res == 'O' :
        score, bonus = score + idx + 1 + bonus, bonus + 1
    else:
        bonus = 0
 
print(score)
cs


훨씬 코드가 간결하다.

파이썬의 각 자료형과 문법의 특성들을 먼저 잘 이해하는게 중요한 것 같다.


변수의 동시 선언 score, bonus = 0, 0

enumerate() 에 대해서 알게 되었다.


C++과 Python 을 마스터해서 코테 깡패가 되어보자..!!

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday