[c++/알고리즘] 프로그래머스 로또의 최고순위와 최저순위

corncheese·2021년 7월 19일
0

알고리즘문제풀이

목록 보기
10/31


#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> lottos, vector<int> win_nums) {
    
    vector<int> answer;
    int val=0, i=0, cnt=0, min=0, max=0, a, b;
    int arr[5] = {6, 5, 4, 3, 2}; // 1개, 0개 인 경우 => index = 6
    
    for(int n=0; n<lottos.size(); n++) {
        if(lottos[n] == 0) {
            // 값이 0 일 때, 다른 값으로 대체될 수 있는 경우
            val++;
        }
        else {
            for(int m=0; m<win_nums.size(); m++){
            	// 일치하는 경우, 이 경우가 최소값이 된다.
                if(lottos[n] == win_nums[m]){ min++; break;}
            }
        }
    }
    
    // 최대값은 0이 모두 다른값으로 대체될 때와 최소로 일치하는 경우의 합
    max = val + min;
    
    // 일치하는 경우가 0,1 인 경우를 대비하여 6일 입력
    answer.push_back(6);
    answer.push_back(6);
    
    for(int j=0; j<5; j++){
    	// 최소, 최대 일치하는 수의 인덱스+1 값 입력
        if(max == arr[j]) {answer[0] = j+1;}
        if(min == arr[j]) {answer[1] = j+1;}
    }
    
    return answer;
}

0개의 댓글