Study hard
(c++) 프로그래머스 코딩테스트 연습 - 모의고사 본문
programmers.co.kr/learn/courses/30/lessons/42840
[풀이]
완전탐색으로 각각 학생의 답안과 정답을 비교하여 맞은 개수를 세고, 가장 많이 맞은 사람을 찾는 문제였다.
가장 높은 점수를 받은 사람을 찾을 때는 Max변수에 최댓값을 저장해두고 비교하면서 같으면 벡터에 넣고, 더 높은 점수가 나오면 벡터를 비운 다음 그 학생의 번호를 넣는 방식으로 풀었다.
#include <string>
#include <vector>
#include <algorithm>
//#include <iostream>
using namespace std;
int ans_cnt;
vector<int>Person[4];
vector<int>High;
int Correct[4];
void getGrade(vector<int> answers){
for(int i=0;i<ans_cnt;i++){
for(int j=1;j<=3;j++){
if(Person[j][i]==answers[i])
Correct[j]++;
}
}
}
void makeA(){
int k=0;
int n=1;
//1번 수포자
while(1){
Person[1].push_back(n);
n++;
if(n==6)
n=1;
k++;
if(k==ans_cnt)
break;
}
k=0;
n=1;
//2번 수포자
while(1){
if(k%2==0)
Person[2].push_back(2);
else{
Person[2].push_back(n);
if(n==1)
n=3;
else
n++;
}
if(n==6)
n=1;
k++;
if(k==ans_cnt)
break;
}
k=1;
n=3;
Person[3].push_back(3);
while(1){
if(k%2==0){
if(n==3)
n=1;
else if(n==1)
n=2;
else if(n==2)
n=4;
else if(n==4)
n=5;
else if(n==5)
n=3;
Person[3].push_back(n);
}
else
Person[3].push_back(n);
k++;
if(k==ans_cnt)
break;
}
}
void getHigh(){
int Max=-1;
for(int i=1;i<=3;i++){
if(Max<Correct[i]){
if(High.size()>0)
High.clear();
Max=Correct[i];
High.push_back(i);
}
else if(Max==Correct[i]){
High.push_back(i);
}
}
if(High.size()>1)
sort(High.begin(),High.end());
}
vector<int> solution(vector<int> answers) {
vector<int> answer;
ans_cnt=answers.size();
makeA();
getGrade(answers);
getHigh();
/*for(int i=0;i<ans_cnt;i++){
cout<<Person[1][i]<<' ';
}
cout<<'\n';
cout<<Correct[1];
*/
return High;
}
+ 각 학생의 찍기 순서를 배열에 저장해두고 1번 학생의 답은 문제번호%5, 2번 학생의 답은 문제번호%8, 3번 학생의 답은 문제번호%10번째 값으로 비교하는 방법
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int Score[4];
//vector<int>Person[4];//각각의 답지
int Person[4][10]={{0},{1,2,3,4,5},{2,1,2,3,2,4,2,5},{3,3,1,1,2,2,4,4,5,5}};
vector<int>Answers;
int problem_cnt;
void fillPaper(){
int one=1;
int two=1;
int three=3;
for(int i=0;i<problem_cnt;i++){
//1번
if(Answers[i]==Person[1][i%5])
Score[1]++;
//2번
if(Answers[i]==Person[2][i%8])
Score[2]++;
//3번
if(Answers[i]==Person[3][i%10])
Score[3]++;
}
}
vector<int> solution(vector<int> answers) {
vector<int> answer;
problem_cnt=answers.size();
Answers=answers;
fillPaper();
int Max=-1;
for(int i=1;i<=3;i++){
cout<<Score[i]<<' ';
}
for(int i=1;i<=3;i++){
if(Max<Score[i]){
Max=Score[i];
answer.clear();
answer.push_back(i);
}
else if(Max==Score[i]){
answer.push_back(i);
}
}
sort(answer.begin(),answer.end());
return answer;
}
'프로그래머스 > 완전탐색' 카테고리의 다른 글
(c++)프로그래머스 코딩테스트 연습 - 카펫 (0) | 2020.10.20 |
---|---|
(c++) 프로그래머스 코딩테스트 연습 - 소수 찾기 (0) | 2020.10.20 |