오늘의 알고리즘
[C++]완전탐색 모의고사(프로그래머스 1레벨)
하늘하늘 .
2022. 2. 3. 15:04
#include <string>
#include <vector>
using namespace std;
int max(int iFirst, int iSecond, int iThird)
{
if (iFirst >= iSecond && iFirst >= iThird)
return iFirst;
else if (iSecond >= iFirst && iSecond >= iThird)
return iSecond;
else
return iThird;
}
vector<int> solution(vector<int> answers)
{
int iFirst = 0;
int iSecond = 0;
int iThird = 0;
int iAnswerSize = answers.size();
vector<int> answer = {};
vector<int> vecFirst = { 1,2,3,4,5 };
int iFirstSize = vecFirst.size();
vector<int> vecSecond = { 2, 1, 2, 3, 2, 4, 2, 5 };
int iSecondSize = vecSecond.size();
vector<int> vecThird = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
int iThirdSize = vecThird.size();
int iAnswer = 0;
for (int i = 0; i < iAnswerSize; ++i)
{
iAnswer = answers[i];
if (vecFirst[i % iFirstSize] == iAnswer)
++iFirst;
if (vecSecond[i % iSecondSize] == iAnswer)
++iSecond;
if (vecThird[i % iThirdSize] == iAnswer)
++iThird;
}
int iMax = max(iFirst, iSecond, iThird);
if (iMax == iFirst)
answer.push_back(1);
if (iMax == iSecond)
answer.push_back(2);
if (iMax == iThird)
answer.push_back(3);
return answer;
}
max 함수를 만들었는데 max_element를 쓰는 게 편했을 듯!
int they_max = *max_element(they.begin(),they.end());