본문 바로가기
PS/프로그래머스

프로그래머스 로또의 최고 순위와 최저 순위 cpp

by 지기_ 2021. 5. 23.
반응형

1. 문제

두 배열을 비교해서 같은 것과 다른 것의 갯수를 비교하는 것.

 

2. 설계

당첨번호를 map에 넣는다. 구매한 로또 배열을 돌면서 있는 값은 지우고 cnt를 증가시킨다. (0번은 없으므로)

최소값은 현재 cnt 최댓값은 cnt+0의 갯수로 리턴한다.

 

3. 알고리즘

 

4. 구현

#include <bits/stdc++.h>
using namespace std;

vector<int> solution(vector<int> lottos, vector<int> win_nums) {
    vector<int> answer;
    map<int,int> mp;
    int cnt=0;
    int zeros=0;
    for(auto iter: win_nums){
        mp[iter]++;
    }
    for(auto iter: lottos){
        if(mp[iter])    cnt++;
        if(iter==0)     zeros++;
    }
    answer.push_back(cnt);
    answer.push_back(cnt+zeros);
    return answer;
}

1) cnt 가 아니라 등수를 넣어야한다.

2) 맞는 것이 1 이하이면 6등 (맞는 것이 0 개여도 6등) cnt와 zeros를 더해도 1이하인 경우 6등을 넣어야한다.

#include <bits/stdc++.h>
using namespace std;

vector<int> solution(vector<int> lottos, vector<int> win_nums) {
    vector<int> answer;
    map<int,int> mp;
    int cnt=0;
    int zeros=0;
    for(auto iter: win_nums){
        mp[iter]++;
    }
    for(auto iter: lottos){
        if(mp[iter])    cnt++;
        if(iter==0)     zeros++;
    }
    
    if(cnt<=1)  answer.push_back(6);
    else    answer.push_back((7-cnt)); 
    
    if((cnt+zeros)<=1)  answer.push_back(6);
    else    answer.push_back((7-(cnt+zeros)));
    
    sort(answer.begin(), answer.end());
    return answer;
}

 

반응형

댓글