C++)
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> answers) {
vector<int> answer;
int scores[3] = {0,};
vector<int> s1 = {1,2,3,4,5};
vector<int> s2 = {2,1,2,3,2,4,2,5};
vector<int> s3 = {3,3,1,1,2,2,4,4,5,5};
// 정답 순회하면서 수포자가 찍은 답과 일치하면 점수 더하기
for(int idx = 0; idx < answers.size(); idx++)
{
if(answers[idx] == s1[idx % s1.size()])
scores[0]++;
if(answers[idx] == s2[idx % s2.size()])
scores[1]++;
if(answers[idx] == s3[idx % s3.size()])
scores[2]++;
}
// 최대값 찾아서 answer에 push
if (scores[0] >= scores[1] && scores[0] >= scores[2]) {
answer.push_back(1);
}
if (scores[1] >= scores[0] && scores[1] >= scores[2]) {
answer.push_back(2);
}
if (scores[2] >= scores[0] && scores[2] >= scores[1]) {
answer.push_back(3);
}
return answer;
}
Python)
def solution(answers):
student1 = [1,2,3,4,5]
student2 = [2,1,2,3,2,4,2,5]
student3 = [3,3,1,1,2,2,4,4,5,5]
arr = [0] * 3
# 채점
for idx in range(len(answers)):
if student1[idx % 5] == answers[idx]:
arr[0] += 1
if student2[idx % 8] == answers[idx]:
arr[1] += 1
if student3[idx % 10] == answers[idx]:
arr[2] += 1
ans = []
for idx, score in enumerate(arr):
if score == max(arr):
ans.append(idx+1)
return ans'프로그래머스 > Level1' 카테고리의 다른 글
| [Level1] 가장 가까운 같은 글자 (C++/Python) (0) | 2023.02.09 |
|---|---|
| [Level 1] 소수 만들기 (C++/Python) (0) | 2023.02.08 |
| [Level1] 콜라 문제 (C++/Python) (0) | 2023.02.07 |
| [Level1] 푸드 파이트 대회 (C++/Python) (0) | 2023.02.07 |
| [Level 1] 시저 암호 (C++/Python) (0) | 2021.01.08 |