1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import sys
sys.stdin = open("input", "rt")
n = int(input())
score = list(map(int, input().split()))
#avg_score = round(sum(score)/n) //***round()는 round_half_even 방식을 취한다.
// 이 말은 44.5000, 65.5 같이 정확히 절반 일 경우 // round()를 하면 짝수쪽을 택하여 44가 되고 66이 된다 // 따라서 round_half_up 방식을 취하고 싶으면 다음의 방식을 따른다 avg_score = int((sum(score)/n)+0.5) // a=a+0.5 -> a=int(a) 를 해주면 int형은 뒤의 소수점을 버린다 min_distance = float('inf')
for idx, _score in enumerate(score):
distance = abs(_score-avg_score) //평균과 가까운 점수를 찾는거니까 거리값을 보면 된다
if min_distance > distance:
min_distance = distance
closest_score = _score
closest_idx = idx+1
elif min_distance == distance: // score가 같은 경우 idx가 더 우선인쪽울 선택하는 조건은 이미 해결되어 있다
if closest_score < _score: // 거리값이 같아도 score가 더 큰 쪽으로 선택
closest_score=_score
closest_idx=idx+1
print("{} {}".format(avg_score, closest_idx))
|
cs |
728x90
'Data Structures & Algorithms' 카테고리의 다른 글
[Algorithms] Basic-소수(에라토스테네스 체) (0) | 2022.05.12 |
---|---|
[Algorithms] Basic-정다면체 문제 (0) | 2022.05.12 |
[Data Structures] 그래프 (Graph) (0) | 2022.05.10 |
[Data Structures] 테이블(Table)과 해쉬(Hash) (0) | 2022.05.09 |
[Data Structures] 탐색 (Search) (0) | 2022.05.09 |