Algorithm/Programmers

[Programmers - lv01] 모의고사 (cpp / python)

  • -
728x90

모의고사

문제 설명

간단한 완전탐색 문제

문제 풀이

소스코드 : C++

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<vector<int>> player = {{1,2,3,4,5},{2,1,2,3,2,4,2,5},{3,3,1,1,2,2,4,4,5,5}};
    vector<int> cnt = {0,0,0};
    for(int i=0; i<answers.size(); i++)
        for(int j=0; j<3; j++)
            if(player[j][i%player[j].size()] == answers[i])cnt[j]++;
    int max_val = *max_element(cnt.begin(),cnt.end());
    for(int i=0; i<cnt.size(); i++)
        if(max_val==cnt[i])
            answer.push_back(i+1);
    sort(answer.begin(),answer.end());
    return answer;
}

소스코드 : Python

def solution(answers):
    answer=[]
    res = [0,0,0]
    player = [[1,2,3,4,5],[2,1,2,3,2,4,2,5],[3,3,1,1,2,2,4,4,5,5]]
    for i in range(len(answers)):
        if answers[i] == player[0][i%len(player[0])]: res[0]+=1
        if answers[i] == player[1][i%len(player[1])]: res[1]+=1
        if answers[i] == player[2][i%len(player[2])]: res[2]+=1
    max_val = max(res)
    for i in range(len(res)):
        if res[i] == max_val:
            answer.append(i+1)
    return sorted(answer)
728x90
300x250
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.