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 함수를 작성해주세요.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
return a.first > b.first;
}
vector<int> solution(vector<int> answers) {
int one[] = {1,2,3,4,5}; //5 * 8
int two[] = {2,1,2,3,2,4,2,5}; //8 * 5
int three[]= {3,3,1,1,2,2,4,4,5,5}; //10 * 4
vector<int> answer;
vector<int> a;
vector<int> b;
vector<int> c;
int x=0;
int y=0;
int z=0;
for(int i =0;i<answers.size();i++)
{
if(answers.at(i)==one[i%5]) x++;
if(answers.at(i)==two[i%8]) y++;
if(answers.at(i)==three[i%10]) z++;
}
vector<pair<int, int>> comp = {{x, 1},{y, 2},{z, 3}};
sort(comp.begin(), comp.end(), compare);
answer.push_back(comp[0].second);
for (int i = 1; i < 3; i++) {
if (comp[i].first == comp[0].first) {
answer.push_back(comp[i].second);
}
else break;
}
return answer;
}
vector<int> a;
vector<int> b;
vector<int> c;
int x=0;
int y=0;
int z=0;
for(int i =0;i<answers.size();i++){
if(answers.at(i)==one[i%5]) x++;
if(answers.at(i)==two[i%8]) y++;
if(answers.at(i)==three[i%10]) z++;
}
vector<pair<int, int>> comp = {{x, 1},{y, 2},{z, 3}};
벡터 집합 처리를 해주면 코드가 더 간결하게 나올 수 있다.
중간을 이어주는 x, y, z를 거쳐갈 필요 없이 바로 진행이 가능하다.vector<int> scores(3); vector<pair<int, int>> comp = {{scores[0], 1}, {scores[1], 2}, {scores[2], 3}};
pair에 값을 배정하는 단계에서도 간결하게 처리 가능하다.
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> answers) {
vector<int> answer;
int one[5] = {1, 2, 3, 4, 5};
int two[8] = { 2, 1, 2, 3, 2, 4, 2, 5};
int three[10] = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int a = 0, b = 0, c = 0; //1, 2, 3번 수포자가 맞춘 개수
int i = 0; //문제의 번호
while(answers.size() != i){
if(answers[i] == one[i % 5])
a++;
if(answers[i] == two[i % 8])
b++;
if(answers[i] == three[i % 10])
c++;
i++;
}
int result = max(max(a, b), c);
if(result == a)
answer.push_back(1);
if(result == b)
answer.push_back(2);
if(result == c)
answer.push_back(3);
return answer;
}
마지막 부분 3개의 수중에서 가장 큰 수를 추출해내는 방법이 너무 신박했다.
나는 for문을 선호하기 때문에 for문을 그대로 사용하고, 그 대신 pair을 쓰지 않고 x, y, z값을 사용해서 max값 적용하는 방식으로 풀면 내 머릿속에도 적용 가능할 것 같은데..
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> answers) {
vector<int> answer;
int one[] = {1,2,3,4,5}; //5 * 8
int two[] = {2,1,2,3,2,4,2,5}; //8 * 5
int three[]= {3,3,1,1,2,2,4,4,5,5}; //10 * 4
int x=0, y=0, z=0;
for(int i =0; i<answers.size(); i++)
{
if(answers.at(i)==one[i%5]) x++;
if(answers.at(i)==two[i%8]) y++;
if(answers.at(i)==three[i%10]) z++;
}
int result = max(max(x,y),z);
if(result == x) answer.push_back(1);
if(result == y) answer.push_back(2);
if(result == z) answer.push_back(3);
return answer;
}
결과적으로 다시 구성해봤다. 훨씬 보기도 편하고 구성자체가 편안하다.
요일의 이름은 일요일부터 토요일까지 각각 SUN,MON,TUE,WED,THU,FRI,SAT
입니다.
예를 들어 a=5, b=24라면 5월 24일은 화요일이므로 문자열 "TUE"를 반환하세요.
제한 조건
2016년은 윤년입니다.
2016년 a월 b일은 실제로 있는 날입니다. (13월 26일이나 2월 45일같은 날짜는 주어지지 않습니다
입출력 예
a b result
5 24 "TUE"
#include <string>
#include <vector>
using namespace std;
string solution(int a, int b) {
string answer = "";
int monthDay[12]={31,29,31,30,31,30,31,31,30,31,30,31};
string week[7] = {"THU", "FRI","SAT","SUN","MON","TUE","WED"};
int sum = b;
for(int i =0;i<a-1;i++)
{
sum += monthDay[i];
}
int i = sum%7;
answer = week[i];
return answer;
}
string date[7] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
date[(sum+4)%7];
위의 방식대로하면 금요일에 시작한다고 하여 배열을 굳이 금요일부터 시작할 필요가 없다.
금요일로 시작하면 헷갈릴 위험이 높기 때문에 달력기준으로 SUN부터 시작하는 방법을 사용했어야했다.
그렇게 만들려면 +4를 해줘서 금요일로 맞춰주면 될 것이다.
생각을 조금만 더 한다면 더 간결한 코드가 가능하다.