문제

Undercut is a card game where two players each have five cards numbered one through five. At each round, each player selects a card, then simultaneously reveals it. If the cards are of equal value, there is no score. Otherwise, there are two cases: the two cards are exactly one point apart (this is called an undercut), or the cards are more than one point apart. In the latter case, the person revealing the larger of the cards gets the number of points on the larger card. In the case of an undercut the player with the lower card gets the sum of the two cards. The exception to this is when the cards are 1 and 2, in which case the player with the lower card gets 6 points (instead of only 3 points). After each round, the cards are returned to the hands and they play another round.

For example, if there are 5 rounds and player A plays (in this order) 5, 3, 1, 3, 5 and player B plays 3, 3, 3, 3, 4, then the scoring for each round would be: A gets 5 points, no points, B gets 3 points, no points, B gets 9 points. The totals would be A: 5, B: 12.

In this problem you will be given card plays for both players and must determine the final scores.

 

입력

There will be multiple input instances. Each instance will be one game. The first line of input for a game will be an integer n <= 20. (A value of n = 0 terminates input.) The next two lines will each contain n integers between 1 and 5 inclusive indicating the cards played on each of n rounds. The first line are player A's card plays and the second line are player B's card plays.

 

출력

Each input instance should generate one line of output of the form:

A has a points. B has b points.

where the value of a and b are for you to determine. A blank line should separate output lines.


코드

import sys

if __name__ == '__main__':

    while True:
        plays = int(sys.stdin.readline().rstrip())
        
        if plays==0:
            break

        A_point = 0
        B_point = 0

        A = list(map(int, sys.stdin.readline().rstrip().split(" ")))
        B = list(map(int, sys.stdin.readline().rstrip().split(" ")))

        for i in range(plays):
            # 한 개 차이면 적은 수를 가진 애가 이김
            # 자신의 카드 넘버와 상대의 카드 넘버 만큼
            if abs(A[i]-B[i]) == 1:
                if A[i] > B[i]:
                    if A[i] == 2:
                        B_point += 6
                    else:
                        B_point += A[i] + B[i]
                else:   # B[i] > A[i]
                    if B[i] == 2:
                        A_point += 6
                    else:
                        A_point += A[i] + B[i]
            elif A[i] < B[i]:
                B_point += B[i]
            elif A[i] > B[i]:
                A_point += A[i]

        print("A has {} points. B has {} points.\n".format(A_point, B_point))

 

풀이

문제를 먼저 해석해보자면, A와 B가 카드게임을 하는데 이때 이기는 조건이 여러 가지이다.

① 두 카드의 숫자 차이가 1보다 클 경우, 숫자가 큰 카드를 가진 사람이 이긴다. 이때, 얻는 점수는 자신의 카드 숫자만큼이다.(큰 카드)

② 두 카드의 숫자 차이가 1일 경우, 숫자가 작은 카드를 가진 사람이 이긴다. 이때, 얻는 점수는 자신의 카드와 상대방의 카드 숫자를 더한 값이다.

③ ②번의 예외로, 두 카드가 1과 2일 경우, 무조건 낮은 카드를 가진 사람이 6점을 얻는다.

④ 두 카드의 숫자가 같을 경우, 무승부로 아무도 점수를 얻지 않는다.

 

위 조건에 맞춰 풀면 된다. 입력을 0이 나올 때까지 받기 때문에 무한 반복을 하고 0이 나오면 break 할 수 있도록 코드를 작성했다. 첫 번째 if 문에서는 A와 B의 카드 차가 1인지 확인했다. 이때 어느 수가 더 큰지 모르기 때문에 abs() 함수를 이용해 절댓값을 씌워서 1인지 확인했다. 그 후, 두 개의 카드가 각각 1과 2라면 6점을 더해주었다. 이때 이미 if 문에서 카드의 차이가 1인지를 확인했기 때문에 A가 B보다 큰 경우, A가 2인지만 확인해주면 B는 자동으로 1이 되므로 A만 조건식에 써주었다. 두 개의 카드가 1과 2가 아니라면 조건 ②에 따라서 두 카드의 합을 점수에 더해주었다.

두 번째와 세 번째 if문의 경우는 조건 ①에 따른 식으로 큰 카드의 수를 점수에 더해주었다.

728x90

'Algorithm > 백준' 카테고리의 다른 글

[백준][Java] 1541. 잃어버린 괄호  (0) 2024.01.31
[백준][Java] 6721. Backward numbers  (0) 2022.04.30
[백준][Java] 1912. 연속합  (0) 2022.04.28
[백준][Python] 19604. Art  (0) 2022.04.25
[백준][Java] 17548. Greetings!  (0) 2022.04.25

+ Recent posts