[프로그래머스/C++] 로또의 최고 순위와 최저 순위

최대 1 분 소요

1.문제 링크

로또의 최고 순위와 최저 순위



2. 풀이 전 계획과 생각

  • 맞출 수 있는 최저개수는 사용자로또와 정답로또가 일치하는 개수, 맞출 수 있는 최고개수는 최저개수+0이다.



3. 풀이

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> lottos, vector<int> win_nums) {
    int low=0, high=0;
    
    for(int i=0;i<lottos.size();i++){
        if(lottos[i]!=0){
            for(int j=0;j<win_nums.size();j++){
                if(lottos[i]==win_nums[j]){
                    low++;
                    break;
                }
            }
        }
        else{
            high++;
        }
    }
    
    high += low;
    
    vector<int> answer;
    
    if(high==6) answer.push_back(1);
    else if(high==5) answer.push_back(2);
    else if(high==4) answer.push_back(3);
    else if(high==3) answer.push_back(4);
    else if(high==2) answer.push_back(5);
    else answer.push_back(6);
    
    if(low==6) answer.push_back(1);
    else if(low==5) answer.push_back(2);
    else if(low==4) answer.push_back(3);
    else if(low==3) answer.push_back(4);
    else if(low==2) answer.push_back(5);
    else answer.push_back(6);

    return answer;
}

low는 맞출 수 있는 최저개수, high는 맞출 수 있는 최고 개수를 저장하는 변수이다.

low의 경우 2중 for문을 돌면서 일치하는 개수를 저장한다.

high의 경우 위에서 2중 for문을 돌면서 0이 나오면 1씩 증가하고 마지막에 low를 더한다.

로또 결과 조건에 맞게 조건문을 작성해서 answer벡터에 삽입한다.



4. 풀이하면서 고민했던 점



5. 문제를 풀고 알게된 개념 및 소감