[백준/Python] 1546)평균

Jimin_Note·어제
0

[백준/Python]

목록 보기
25/27
post-thumbnail

✨ 2025.07.11 코딩테스트 문제 풀이

📌 문제: 평균

🧠 문제 설명

  • 입력 : 과목 수 N, 그리고 N개의 점수
  • 처리 : 점수들을 점수 / 최고 점수 * 100 방식으로 변환
  • 출력 : 변환된 점수들의 평균

💻 내가 작성한 코드

  • ❌1번째 답안(실패)
import sys
import statistics
 
N = int(sys.stdin.readline())
score = list(map(int, sys.stdin.readline().split()))
sorted_score = sorted(score, reverse=True)

for i in range(len(sorted_score)):
    sorted_score[i] = sorted_score[i] / sorted_score[0] * 100 
# 이 부분이 문제!

print(statistics.mean(sorted_score))
  • sort를 해서 sorted_score 리스트를 내림차순으로 변경 후 sorted_score[0]를 기준으로 정규화하려 했지만, 루프 도중 sorted_score[0] 값이 변경되면서 잘못된 계산 발생
    -> 기준값(max)을 별도 변수에 저장해서 고정해야 했었다.
  • 위 코드를 살리려면 아래와 같이 구현해야함
import sys
import statistics
 
N = int(sys.stdin.readline())
score = list(map(int, sys.stdin.readline().split()))
M = max(score)

normalized = [i/ M* 100 for i in score ]


print(statistics.mean(normalized)
  • ⭕️ 2번째 답안(통과)
import sys
import statistics

N = int(sys.stdin.readline())
score = list(map(int, sys.stdin.readline().split()))
sorted_score = sorted(score, reverse=True)

normalized = [ i / sorted_score[0] * 100 for i in sorted_score]

print(statistics.mean(normalized))
  • 처음엔 max() 대신 sorted(score, reverse=True)[0] 방식으로 리스트를 재생성한 후 값을 하나씩 계산해서 빈 리스트에서 append 하는 방식으로 통과했음
  • 성능이나 의도 측면에서는 max()를 쓰는 게 더 직관적이고 안전하지만, 이 방법도 정답 판정은 됐다.

✅ 배운 점

  • 루프 내에서 기준값으로 list[0]을 사용하면, 리스트 값이 변경되면서 기준도 변할 수 있다.
    -> 기준값은 꼭 for 루프 외부에서 고정 변수로 저장해서 써야 안정적이다.
  • import statistics 모듈의 statistics.mean() 함수는 리스트, 튜플 등의 평균을 쉽게 구할 수 있는 내장 도구이다.
profile
Hello. I'm jimin:)

0개의 댓글