알고리즘 풀이/프로그래머스
프로그래머스 - 모의고사
100win10
2019. 11. 5. 06:37
문제 : https://programmers.co.kr/learn/courses/30/lessons/42840
코딩테스트 연습 - 모의고사 | 프로그래머스
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 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,
programmers.co.kr
풀이 :
1,2,3번 (a,b,c) 배열을 각각 만들고 그들의 찍는 방식을 기록해둔다.
그 후 answers 배열의 길이만큼 for문을 돌리면서 찍은게 맞췄다면 score을 하나씩 높여주자
v 벡터에는 {점수와 학생}이 담긴다. 비교는 compare 함수를 통해 점수가 같다면 학생 번호를
오름차순으로 정렬하도록 해주자.
코드(C++)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int a[5] = { 1,2,3,4,5 }; | |
int b[8] = { 2,1,2,3,2,4,2,5 }; | |
int c[10] = { 3,3,1,1,2,2,4,4,5,5 }; | |
bool compare(pair<int, int > a, pair<int, int> b) | |
{ | |
if (a.first == b.first) | |
{ | |
return a.second < b.second; | |
} | |
else | |
return a.first > b.first; | |
} | |
vector<int> solution(vector<int> answers) { | |
vector<int> answer; | |
int aScore = 0; | |
int bScore = 0; | |
int cScore = 0; | |
for (int i = 0; i < answers.size(); ++i) | |
{ | |
if (answers[i] == a[i%5]) aScore++; | |
if (answers[i] == b[i%8]) bScore++; | |
if (answers[i] == c[i%10]) cScore++; | |
} | |
vector<pair<int, int >> v; | |
v.push_back({ aScore,1 }); | |
v.push_back({ bScore,2 }); | |
v.push_back({ cScore,3 }); | |
sort(v.begin(), v.end(), compare); | |
int ret = v[0].first; | |
answer.push_back(v[0].second); | |
for (int i = 1; i < v.size(); ++i) | |
{ | |
if (v[i].first == ret) | |
answer.push_back(v[i].second); | |
} | |
return answer; | |
} |