Algorithm/프로그래머스
[프로그래머스/C++] 모의고사
pinevienna
2021. 2. 7. 23:54
늘 같은 패턴으로 찍는 수포자1, 2, 3 중에 제일 점수가 높은 놈을 return하는 문제
수포자별로 배열을 하나씩 만들어서 찍는 패턴 그대로 배열에 넣어준다
그리고 나머지(%) 연산으로 정답과 비교해줬다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; vector<int> solution(vector<int> answers) { vector<int> answer; int arr1[5] = { 1,2,3,4,5 }; int arr2[8] = { 2,1,2,3,2,4,2,5 }; int arr3[10] = { 3,3,1,1,2,2,4,4,5,5 }; int cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int i = 0; i <= answers.size(); i++) { if (arr1[i % 5] == answers[i]) cnt1++; if (arr2[i % 8] == answers[i]) cnt2++; if (arr3[i % 10] == answers[i]) cnt3++; } int max_val = max({ cnt1,cnt2,cnt3 }); if (cnt1 == max_val) answer.push_back(1); if (cnt2 == max_val) answer.push_back(2); if (cnt3 == max_val) answer.push_back(3); return answer; } | cs |
뭔가 쓰고보니 깔끔한듯 묘하게 더러운게 걸리지만..ㅎㅎ