[백준 4344 파이썬] - 평균은 넘겠지

zsunny·2022년 6월 22일
0

📌 문제

💯 정답

방법 1.

import sys
input = sys.stdin.readline

c = int(input())

for i in range(c):
    scores = list(map(int, input().split()))
    avg_s = sum(scores[1:])/scores[0]
    cnt = 0
    for j in range(1, len(scores)):
        if scores[j] > avg_s:
            cnt += 1
    result = round(cnt / scores[0] * 100, 3)
    print(f"{result:.3f}%")

방법 2. numpy 사용 ❌ 런타임 에러

import sys
import numpy
input = sys.stdin.readline

c = int(input())

for i in range(c):
    scores = list(map(int, input().split()))
    avg_s = numpy.mean(scores[1:])
    cnt = 0
    for j in scores[1:]:
        if j > avg_s:
            cnt += 1
    result = round(cnt / scores[0] * 100, 3)
    print(f"{result:.3f}%")

📝 설명

• 리스트인 scores는 ✨scores[0]=전체 학생수, scores[1:]=점수✨를 의미한다.
• j가 1부터 len(scores)일때까지 반복하며 scores[j]의 점수가 평균 점수(avg_s)보다 큰 경우에 cnt를 1 증가시킨다.
• 평균보다 높은 학생수(cnt) / 전체 학생수 X 100 을 하고 round로 반올림해 소수 3째자리로 나타내준다.
• f-string 방법을 이용해 소수점 3자리로 자릿수를 지정해준다

⭐️ 알고가기 _ numpy / round / f-string

🔎 numpy

  • numpy는 pip install numpy를 이용해 설치하는 외부 모듈이다.
  • 백준은 파이썬 설치시 기본으로 깔려있는 표준 라이브러리 외는 지원하지 않는다.
  • 따라서 numpy사용시 '런타임 에러'가 뜨게 된다.

🔎 소수점 구하기 (round, f-string)

👉 반올림/올림/내림/버림, f-string 설명 바로가기

profile
매일 성장하는 예비 웹 개발자 🌱

0개의 댓글