BOJ/Python

[BOJ/백준] 2966 - 찍기 (Python)

tjdms4327 2024. 11. 6. 10:58

문제

문제링크

상근이( Adrian )는 A, B, C, A, B, C, A, B, C, A, B, C, ...로, 창영이( Bruno )는 B, A, B, C, B, A, B, C, B, A, B, C, ...로,

현진이( Goran )는 C, C, A, A, B, B, C, C, A, A, B, B, ...로 문제의 답을 찍는다.

문제의 수와 정답이 주어질 떄, 가장 많이 맞힌 사람과 그 수를 구하라.

 

풀이

각 사람이 찍는 방식에 따라 if문을 구성하여 문제가 맞으면 각 사람의 value값에 1을 더한다.

이때 나중에 사람 이름과 맞힌 문제 수를 모두 출력해야 하기 편리하도록 dictionary를 사용했다.

모든 문제와 사람에 대해 정답 판단이 끝난 후 dictionary의 value값에 대해 최대값을 구한다.

value값이 이 최대값과 같은 경우에 key값을 승자로 판단하고, 이에 해당하는 모든 인원을 출력하게 된다.

 

코드

def find_winner(n, answer):
    palyers={'Adrian':0, 'Bruno':0, 'Goran':0}
    for i in range(n):
        if (i%3==0 and answer[i]=='A') or (i%3==1 and answer[i]=='B') or (i%3==2 and answer[i]=='C'):
            palyers['Adrian']+=1
        if (i%2==0 and answer[i]=='B') or (i%4==1 and answer[i]=='A') or (i%4==3 and answer[i]=='C'):
            palyers['Bruno']+=1
        if  ((i%6==0 or i%6==1) and answer[i]=='C') or ((i%6==2 or i%6==3) and answer[i]=='A') or ((i%6==4 or i%6==5) and answer[i]=='B'):
            palyers['Goran']+=1

    max_score = max(palyers.values())
    winners = [key for key, val in palyers.items() if val == max_score]
    print(max_score)
    print(*winners, sep='\n')

        
n=int(input())
answer=input()
find_winner(n, answer)