CT/SWEA

[SWEA][D2][Python] 1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기

hyunji1109 2023. 6. 8. 23:19

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV13zo1KAAACFAYh&categoryId=AV13zo1KAAACFAYh&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=3

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

1000명의 학생수의 수학성적의 최빈수를 출력한다.

단, 최빈수가 여러 개인 경우 가장 큰 점수를 출력한다.


  1. 101크기의 count리스트를 만든다.
  2. arr리스트에 학생 성적이 입력되면 해당 성적을 index로 하는 count값을 1 증가시킨다.
  3. count와 최빈수(max)가 같은 값을 확인한다.
  4. 최빈수 값이 여러개인 경우 더 큰 값을 fans로 지정한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
= int(input())
for test_case in range(1, T + 1):
    tc= int(input())
    arr= list(map(int,input().split()))
    count=[0]*101
    
    for i in arr:
        for j in range(101):
            if i == j:
                count[j] += 1
                
    ans = 0
    fans = 0
    
    for i in range(101):
        if count[i] == max(count):
            ans = i
            if ans > fans:
                fans = ans  
         
    print(f'#{test_case} {fans}')
 
cs