Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

NISSO

[백준 2108] 시간초과와 런타임에러 본문

Coding Test

[백준 2108] 시간초과와 런타임에러

oniss 2021. 6. 27. 01:23
a,l = int(input()), []
for _ in range(a):
    l.append(int(input()))
l.sort()

from collections import Counter
c = Counter(l).most_common()
m = c[0][0] if len(c)<2 or c[0][1] != c[1][1] else c[1][0]
print('%d\n'*4 %(round(sum(l)/len(l),0), l[(a-1)//2], m, l[a-1]-l[0]))

 

내 코드였고 시간초과가 떴다.

각 계산한 값을 변수에 저장하고 print문을 간소화했는데 이번엔 런타임 에러가 떴다.

 

a,l = int(input()), []
for _ in range(a):
    l.append(int(input()))
l.sort()

from collections import Counter
c = Counter(l).most_common()

x = round(sum(l)/len(l),0)     #1
y = l[(a-1)//2]  			   #2
m = c[0][0] if len(c)<2 or c[0][1] != c[1][1] else c[1][0]
z = [a-1]-l[0] 			       #3
print('%d\n'*4 %(x, y, m, z))  #4

 

입력 받는 방식을 바꿔서 해결해보려고 했는데 아래와 같이 sys.stdin.readline()도 안 된다.

 

import sys

n = list(sys.stdin.readline())
print(n)

 

처음엔 아예 [Errno 9] Bad file descriptor 이 에러가 뜨더니 갑자기 또 되긴 한다.

된다고 해야하나, 하여튼 입력창이 안 뜨고 그냥 끝난다.

구글링해봐도 이유를 모르겠고.. 잠이나 자야겠다.

내일 해결하면 아래 추가하도록 하겠다.


 

import sys
a,l = int(input()), []
for _ in range(a):
    l.append(int(sys.stdin.readline()))  #수정
l.sort()

from collections import Counter
c = Counter(l).most_common()
m = c[0][0] if len(c)<2 or c[0][1] != c[1][1] else c[1][0]
print('%d\n'*4 %(round(sum(l)/len(l),0), l[(a-1)//2], m, l[a-1]-l[0]))

 

input()에서 sys.stdin.readline()으로 바꿔 제출하니 바로 해결됐다.

 

코랩에서는 int(sys.stdin.readline())으로 실행했을 때 invalid literal for int() with base 10: '' 에러가 뜬다.

sys.stdin.readline()는 Spyder IDE에서 작동하지 않는다고 한다.

기본 파이썬 쉘 IDE를 사용해서 해결하면 된다.

Comments