구름 알고리즘 위클리 [2주차 문제 2]
문제 정리
사용할 파라미터 :
t 게임 진행 횟수
v 각 게임에 사용되는 사탕의 수
코드
t=int(input())
v=[]
try:
v.append(list(map(int, input().strip().split(" "))))
if len(v[0])!=t:
raise(Exception)
except Exception as e:
print("게임 횟수에 맞도록 사탕 개수를 입력하시오.")
win_goorm=0
win_friend=0
for i in range(len(v[0])):
if v[0][i]%2==0:
win_friend+=1
else:
win_goorm+=1
if win_goorm==win_friend:
print("tie")
else:
print(max(win_goorm, win_friend))
코드 설명
t=int(input())
v=[]
try:
v.append(list(map(int, input().strip().split(" "))))
if len(v[0])!=t:
raise(Exception)
except Exception as e:
print("게임 횟수에 맞도록 사탕 개수를 입력하시오.")
게임횟수t와 게임에 사용되는 사탕의 수V를 입력받음.이때, 사탕이 제공되는 횟수와 게임횟수가 틀리면 에러 발생.
win_goorm=0
win_friend=0
for i in range(len(v[0])):
if v[0][i]%2==0:
win_friend+=1
else:
win_goorm+=1
구름이 이기는 조건인 사탕의 개수가 홀수이면 win_goorm이 +1를, 친구가 이기는 조건인 사탕의 개수가 짝수이면 win_friend가 +1이된다.
if win_goorm==win_friend:
print("tie")
else:
print(max(win_goorm, win_friend))
더 많이 이긴 사람의 횟수를 출력하라고 하였으므로 max함수를 사용.
비기면 tie를 출력
출력 예시