https://programmers.co.kr/learn/courses/30/lessons/77484
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
문제 요약 >
로또 종이가 오염되었다. 당첨 가능한 최고 순위와 최저 순위를 구하라.
풀이 >
0의 개수(zeroCnt)와 맞은 숫자 개수(commonCnt)를 센다.
0~45까지의 인덱스를 가지는 배열(array)을 이용하여, 인덱스가 0인 경우 zeroCnt++를 수행하고
배열의 값이 2인 경우 commonCnt(lottos와 win_nums의 중복된 값)++을 수행한다.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define SELECTED_COUNT 6
#define LOTTO_COUNT 46
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
int count[LOTTO_COUNT] = {0,};
int i = 0;
int commonCnt = 0;
int zeroCnt = 0;
for (i = 0; i < SELECTED_COUNT; ++i)
{
count[lottos[i]]++;
count[win_nums[i]]++;
}
for (i = 0; i < LOTTO_COUNT; ++i)
{
if (0 == i) zeroCnt += count[i];
else if (2 == count[i]) commonCnt++;
}
vector<int> answer;
answer.push_back(min(7 - commonCnt - zeroCnt, 6)); // best
answer.push_back(min(7 - commonCnt, 6)); // worst
return answer;
}
'PROBLEM SOLVING > 프로그래머스' 카테고리의 다른 글
프로그래머스 / 더 맵게 (0) | 2022.01.31 |
---|---|
프로그래머스 / 완주하지 못한 선수 (0) | 2022.01.25 |
프로그래머스 / 문자열 압축 (0) | 2022.01.18 |
프로그래머스 / 숫자 문자열과 영단어 (0) | 2022.01.17 |
프로그래머스 / 신규 아이디 추천 (0) | 2022.01.17 |