[#94][알고리즘][SW Expert Academy] 1204. S/W 문제해결 기본 1일차 - 최빈수 구하기

[SW Expert Academy] 1204. S/W 문제해결 기본 1일차 - 최빈수 구하기

문제 링크 (https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh&categoryId=AV13zo1KAAACFAYh&categoryType=CODE)

C++풀이
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    for (int i = 1; i <= 10; i++) {
        int max = 0, maxindex = 0, input[1001= { 0 }, testCase;
        cin >> testCase;
        vector<int> answer;
        answer.resize(101);
    
        //학생 1000명에 대한 점수 입력
        for (int j = 1; j <= 1000; j++) {
            cin >> input[j];
            answer.at(input[j])++;
        }
        //vector에서 제일 큰(최빈수) 찾기
        int ans = *max_element(answer.begin(), answer.end());
        for (int k = 100; k >= 1; k--) {
            //최빈수가 여러 개일 경우 큰 수를 리턴해야 하므로 뒤에서부터 찾기
            if (answer.at(k) == ans) {
                cout << "#" << testCase << " " << k << " " << endl;
                break;
            }
        }
    }
    return 0;
}
cs

댓글

댓글 쓰기

가장 많이 본 글