본문 바로가기
백준/Python

[Python] 백준 4344번: 평균은 넘겠지

by ewaterland 2023. 5. 15.

코드

C = int(input())  # The number of test case

for i in range(C):
  list_temp = list(map(int, input().split()))
  N = list_temp[0]  # The number of student
  scores = list_temp[1:]  # The students's score
  sum = 0
  avg = 0
  count = 0  # the number of students who scored higher than avg

  # Calculating the avg
  for score in scores:
    sum += score
  avg = sum/N
  
  # Calculating the percentage of students who scored higher than avg
  for i in range(N):
    if (scores[i] > avg):
      count += 1
  print("{:.3f}%".format(round((count/N)*100, 3)))

풀이

한 줄에 학생들의 수와 학생들의 점수를 모두 입력받아야 하기 때문에 하나의 리스트로 입력받은 후 인덱싱을 통해 나눠줬다.

 

하나의 for문을 돌릴 때 계산을 모두 끝내고, 바로 결과가 출력되었으면 해서 입력받은 후 바로 평균을 계산했다.

 

평균 점수를 넘는 학생의 수를 카운팅 하는 for문이 끝나면 비율 계산하여 출력되게끔 하였다.

소수점 셋째자리까지 반올림 해야 되기 때문에 round() 함수를 써 주었다.