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

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
     answer.clear();
    int first[5]={1,2,3,4,5}; 
    int firstScore=0;
    int second[8]={2,1,2,3,2,4,2,5};  
    int secondScore=0;
    int third[10]={3,3,1,1,2,2,4,4,5,5}; 
    int thirdScore=0;
    for(int i=0;i<answers.size();i++){
   
            if(answers[i]==first[i%5]){
                firstScore++;
            }
         
            if(answers[i]==second[i%8]){
                secondScore++;
            }
       
            if(answers[i]==third[i%10]){
                thirdScore++;
           }

    }
   
    
    if(firstScore>secondScore&&firstScore>thirdScore){
        answer.push_back(1);
      
        
    }else if(secondScore>firstScore&&secondScore>thirdScore){
         answer.push_back(2);
   
    }else if(thirdScore>firstScore&&thirdScore>secondScore){
        answer.push_back(3);
       
    }else if(firstScore==secondScore&&firstScore>thirdScore){
        answer.push_back(1);
        answer.push_back(2);
       
    }else if(firstScore==thirdScore&&firstScore>secondScore){
        answer.push_back(1);
        answer.push_back(3);
      
    }else if(secondScore==thirdScore&&firstScore<thirdScore){
        answer.push_back(2);
        answer.push_back(3);
      
    }else if(secondScore==thirdScore&&firstScore==thirdScore){
        answer.push_back(1);
        answer.push_back(2);
        answer.push_back(3);
      
    }
    
  
    
    return answer;
}

수정 : 200826

#include <string>
#include <vector>
#include <algorithm>
#include<iostream>
using namespace std;
struct SAVES{
      
  int stuScore;
  int num;  
    
};
bool cmp(const SAVES &p1, const SAVES &p2){
    if(p1.stuScore < p2.stuScore){
        return true;
    }
    else if(p1.stuScore== p2.stuScore){
        return p1.stuScore < p2.stuScore;
    }
    else{
        return false;
    }
}

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<SAVES>assert;
    SAVES stuScore1;
    SAVES stuScore2;
    SAVES stuScore3;
    int stu1[5]={1,2,3,4,5};
    stuScore1.stuScore=0;
    stuScore1.num=1;
    
    int stu2[8]={2, 1, 2, 3, 2, 4, 2, 5};
    stuScore2.stuScore=0;
    stuScore2.num=2;
    
    int stu3[10]={3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
    stuScore3.stuScore=0;
    stuScore3.num=3;
    for(int i=0;i<answers.size();i++){
        if(stu1[i%5]==answers[i]){
            stuScore1.stuScore++;
        }
        if(stu2[i%8]==answers[i]){
            stuScore2.stuScore++;
            
        }if(stu3[i%10]==answers[i]){
            
            stuScore3.stuScore++;
        }
    }
    assert.push_back(stuScore1);
    assert.push_back(stuScore2);
    assert.push_back(stuScore3);
    
    sort(assert.begin(),assert.end(),cmp);
    
    if(assert[2].stuScore==assert[1].stuScore){
        if(assert[2].stuScore==assert[0].stuScore){
        answer.push_back(1);
        answer.push_back(2);
        answer.push_back(3);
        }else{
        answer.push_back(assert[1].num);
        answer.push_back(assert[2].num);
        }
        
    }else{
    
   
    answer.push_back(assert[2].num);
  }
    
    
    
    return answer;
}

 

이번에 sorting을 해서 해봣는데 이전 코드보다는 짧지만 그래도 길다 를 조금 더 줄이는 방법을 알아내고 싶다 ㅠ

+ Recent posts