👀 문제 설명
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.
1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.
제한 조건
- 시험은 최대 10,000 문제로 구성되어있습니다.
- 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
- 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.
입출력 예
answers | return |
[1, 2, 3, 4, 5] | [1] |
[1, 3, 2, 4, 2] | [1, 2, 3] |
✍🏻풀이
각 수포자의 반복되는 답을 각각의 배열에 넣어둔다.
int p1[5] = { 1, 2, 3, 4, 5 };
int p2[8] = { 2, 1, 2, 3, 2, 4, 2, 5 };
int p3[10] = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
정답이 들어있는 answers 벡터의 처음부터 끝까지 접근하여 인덱스를 차례대로 받아온다.
해당 인덱스를 각 수포자의 배열 크기로 나눈 나머지값을 가지고, 해당 위치의 수포자의 답과 모의고사의 답(찐답)을 비교해서 맞으면 각 수포자의 정답률을 +1 해준다.
그리고 수포자의 정답률을 비교!
그런데 내 코드는 굳이 굳이 돌려돌려 돌림판으로 복잡한 방법을 사용한 것 같다,,!
다른 사람의 풀이를 보니 푸는 방법은 똑같은데 맞췄는지 판단하는 방법을 아주 간단하게 짰고, max_element라는 함수를 이용해 가장 많이 맞춘 사람의 정답수를 구했다!
그리고 내 답은 각 수포자의 정답률을 각각 int형 변수 p1CorrectCount, p2CorrectCount, p3CorrectCount로 했는데, 다른 풀이는 각각의 정답률을 가진 배열을 만들어 거기에 정답률을 넣어뒀다! 그래서 이 배열과 max_element 함수를 사용해 훨씬 쉽게 구한 것이다..!
내 코드
//
// Programmers_42840.cpp
// Algorithm
//
// Created by 조수민 on 2020/10/26.
// Copyright © 2020 조수민. All rights reserved.
//
// 모의고사(https://programmers.co.kr/learn/courses/30/lessons/42840)
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int p1[5] = { 1, 2, 3, 4, 5 };
int p2[8] = { 2, 1, 2, 3, 2, 4, 2, 5 };
int p3[10] = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
/**
각 수포자가 찍는 방식을 int형 배열에 각각 저장해두고,
answers 인덱스를 각 수포자의 배열 크기로 나눠 나머지로 몇번째에 있는지 판별,,?
*/
bool isCorrect(int ansNum, int ansIndex, int personNum) {
bool correct = false;
switch (personNum) {
case 1:
{
int index = ansIndex % 5;
if (ansNum == p1[index])
correct = true;
break;
}
case 2:
{
int index = ansIndex % 8;
if (ansNum == p2[index])
correct = true;
break;
}
case 3:
{
int index = ansIndex % 10;
if (ansNum == p3[index])
correct = true;
break;
}
default:
break;
}
return correct;
}
vector<int> solution(vector<int> answers) {
vector<int> answer;
int p1CorrectCount = 0;
int p2CorrectCount = 0;
int p3CorrectCount = 0;
for (int i = 0; i < answers.size(); i++) {
int correct1 = isCorrect(answers.at(i), i, 1);
int correct2 = isCorrect(answers.at(i), i, 2);
int correct3 = isCorrect(answers.at(i), i, 3);
if (correct1 == true)
p1CorrectCount++;
if (correct2 == true)
p2CorrectCount++;
if (correct3 == true)
p3CorrectCount++;
}
vector<pair<int, int>> personalCorrectCount;
personalCorrectCount.push_back(make_pair(p2CorrectCount, 2));
personalCorrectCount.push_back(make_pair(p3CorrectCount, 3));
personalCorrectCount.push_back(make_pair(p1CorrectCount, 1));
sort(personalCorrectCount.begin(), personalCorrectCount.end(), greater<>()); // 내림차순 정렬
int maxCount = personalCorrectCount.at(0).first;
answer.push_back(personalCorrectCount.at(0).second);
for (int i = 1; i < personalCorrectCount.size(); i++) {
if (personalCorrectCount.at(i).first == maxCount) {
answer.push_back(personalCorrectCount.at(i).second);
}
}
sort(answer.begin(), answer.end(), less<>());
return answer;
}
int main() {
vector<int> ss = solution({ 1, 3, 2, 4, 2 });
for (int i = 0; i < ss.size(); i++) {
cout << ss.at(i) << " ";
}
return 0;
}
다른 풀이
//
// Programmers_42840_using_max_element().cpp
// Algorithm
//
// Created by 조수민 on 2020/10/26.
// Copyright © 2020 조수민. All rights reserved.
//
// 모의고사(https://programmers.co.kr/learn/courses/30/lessons/42840)
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> one = { 1, 2, 3, 4, 5 };
vector<int> two = { 2, 1, 2, 3, 2, 4, 2, 5 };
vector<int> thr = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
vector<int> solution(vector<int> answers) {
vector<int> answer;
vector<int> correctCount(3);
for(int i = 0; i < answers.size(); i++) {
if(answers[i] == one[i % one.size()])
correctCount[0]++;
if(answers[i] == two[i % two.size()])
correctCount[1]++;
if(answers[i] == thr[i % thr.size()])
correctCount[2]++;
}
int they_max = *max_element(correctCount.begin(),correctCount.end());
for(int i = 0; i < 3; i++) {
if(correctCount[i] == they_max)
answer.push_back(i + 1);
}
return answer;
}
'숨막히는 알고말고 > 문제 풀이' 카테고리의 다른 글
[Programmers] 카펫 (0) | 2020.11.10 |
---|---|
[Programmers] 소수 찾기 (0) | 2020.10.28 |
[Programmers] H-Index (0) | 2020.10.25 |
[Programmers] 가장 큰 수 (0) | 2020.10.25 |
[Baekjoon] 순열 사이클(BOJ 10451) (0) | 2020.10.23 |