Study hard

(c++) 프로그래머스 코딩테스트 연습 - 모의고사 본문

프로그래머스/완전탐색

(c++) 프로그래머스 코딩테스트 연습 - 모의고사

Nimgnoej 2020. 10. 19. 20:46

programmers.co.kr/learn/courses/30/lessons/42840

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는 ��

programmers.co.kr

[풀이]

완전탐색으로 각각 학생의 답안과 정답을 비교하여 맞은 개수를 세고, 가장 많이 맞은 사람을 찾는 문제였다.

가장 높은 점수를 받은 사람을 찾을 때는 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;
}