[완전탐색] 모의고사

yyeahh·2020년 8월 9일
0

프로그래머스

목록 보기
12/35

|| 문제설명 ||

  1. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 한다.
  2. 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성하라.
  • answers : 1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열
_ 시험 : 최대 10,000 문제
_ 문제의 정답 :  1, 2, 3, 4, 5중 하나
_ 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬

|| 문제해결과정 ||

- 1번 방식 : {1, 2, 3, 4, 5}, 5개
- 2번 방식 : {2, 1, 2, 3, 2, 4, 2, 5}, 8개
- 3번 방식 : {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}, 10개
- int cnt[3] : 수포자들의 정답수
- for문(i) : 0 ~ answers.size()

- if (answer[i] == 수포자들) cnt[수포자들]++
- max 구하기
- for문(i) : 0 ~ 3
if (cnt == max) answer.push_back(i+1)


|| 코드 ||

[2020.08.09] 성공

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    int cnt[3] = {0}, max = 0;
    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};
    
    for(int i=0; i<answers.size(); i++) {
        if(answers[i] == one[i%5]) cnt[0]++;
        if(answers[i] == two[i%8]) cnt[1]++;
        if(answers[i] == three[i%10]) cnt[2]++;
    }
    for(int i=0; i<3; i++) {
        if(max < cnt[i]) max = cnt[i];
    }
    for(int i=0; i<3; i++) {
        if(cnt[i] == max) answer.push_back(i+1);
    }
    return answer;
}


[2021.02.07]
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    int cnt[3] = {0}, max = 0,
        one[5] = {1, 2, 3, 4, 5},
        two[8] = {2, 1, 2, 3, 2, 4, 2, 5},
        three[10] = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
    
    for(int i = 0; i < answers.size(); i++) {
        if(answers[i] == one[i % 5]) cnt[0]++;
        if(answers[i] == two[i % 8]) cnt[1]++;
        if(answers[i] == three[i % 10]) cnt[2]++;
    }
    for(int i = 0; i < 3; i++)
        if(max < cnt[i]) max = cnt[i];
    
    if(cnt[0] == max) answer.push_back(1);
    if(cnt[1] == max) answer.push_back(2);
    if(cnt[2] == max) answer.push_back(3);
    
    return answer;
}

0개의 댓글