Score Calculation

Tristan·2024년 6월 30일

The OX question refers to a question that has two answers correct or incorrect. In the case of consecutive answers in a test consisting of multiple OX questions, the score was calculated as follows to give additional points. If question 1 is correct, calculate it as 1 point. For the previous question, the first question that is correct while getting the answer wrong is calculated as 1 point. In addition, if the answer is correct in succession, the second question is calculated as 2, the third question is 3, ..., and the K question is calculated as K. The wrong question is calculated as 0 points.

For example, if the answer is correct in 10 OX questions as shown below, the score calculation is calculated as shown in the table below, and if it is incorrect, the total score is 1+1+2+3+1+2=10.

1 0 1 1 1 0 0 1 1 0

When the scoring results of the test questions are given, write a program to calculate the total score.

Input:

The first line is given the number of questions N (1 ≤ N ≤ 100). In the second line, 0 or 1 representing the scoring of N questions is given with a blank space between them. 0 is the case where the answer is incorrect, and 1 is the case where the answer is correct.

Output:

Prints a prime number on the first line.

Input Example:

20

Output Example:

In the first line, the total score considering additional points is output for the scoring result given in the input.

Solution

  • we need two variables cnt (count the score) and sum (sum of the scores)
  • as for loop goes through the list a, cnt will increase by 1 if i == 1
    - ex) 1 0 1 1 1, score becomes +1, +0, +1, +2, +3
  • sum will add up the cnt as it goes through the for loop
  • if i != 1, cnt will become 0
n = int(input())
a = list(map(int, input().split()))

cnt = 0
sum = 0

for i in a:
    if i == 1:
        cnt += 1
        sum += cnt
    
    else: 
        cnt = 0
        
print (sum)
profile
@Columbia

0개의 댓글