problem solving
[프로그래머스]모의고사
Shinuk Yi
2020. 3. 6. 09:02
mod를 이용해 각 유저의 답을 순회할 수 있도록 했다.
답 확인이 끝난 후 정답의 횟수에 따라 조건문을 진행함으로써 answer을 구했다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int user1[5] = { 1, 2, 3, 4, 5};
int user2[8] = { 2, 1, 2, 3, 2, 4, 2, 5 };
int user3[10] = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
vector<int> solution(vector<int> answers) {
vector<int> answer;
int count[3] = { 0 };
for(int i = 0; i < answers.size(); i++) {
if(answers[i] == user1[i % 5]) {
count[0]++;
}
if(answers[i] == user2[i % 8]) {
count[1]++;
}
if(answers[i] == user3[i % 10]) {
count[2]++;
}
}
answer.push_back(1);
if(count[0] < count[1]) {
answer.clear();
answer.push_back(2);
}
else if(count[0] == count[1]) {
answer.push_back(2);
}
if(count[answer[0] - 1] < count[2]) {
answer.clear();
answer.push_back(3);
}
else if(count[answer[0] - 1] == count[2]) {
answer.push_back(3);
}
return answer;
}
|
cs |