입출력 예
lottos | win_nums | result |
[44, 1, 0, 0, 31, 25] | [31, 10, 45, 1, 6, 19] | [3, 5] |
[0, 0, 0, 0, 0, 0] | [38, 19, 20, 40, 15, 25] | [1, 6] |
[45, 4, 35, 20, 3, 9] | [20, 9, 3, 45, 4, 35] | [1, 1] |
0은 알 수 없는 번호일 때, 나올 수 있는 최고 순위/최저 순위를 출력하는 코드 작성하기.
순위 | 당첨 내용 |
1 | 6개 번호가 모두 일치 |
2 | 5개 번호가 일치 |
3 | 4개 번호가 일치 |
4 | 3개 번호가 일치 |
5 | 2개 번호가 일치 |
6(낙첨) | 그 외 |
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer;
return answer;
}
[ 풀이 ]
- 만약 lottos에 있는 한 번호가 win_nums에 존재하는 번호라면, 그 번호는 무조건 모두 일치하는 번호이다.
- 0일 경우, 번호를 일치하게 만들거나 일치하지 않게 만드는 것이 모두 가능하다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer(2, 0);
for(int item : lottos)
{
if(item == 0)
answer[0]++;
else
{
if(std::find(win_nums.begin(), win_nums.end(), item) != win_nums.end())
{
answer[0]++;
answer[1]++;
}
}
}
for(int& item : answer)
{
item = (lottos.size() - item) + 1;
if(item > 6) item = 6;
}
return answer;
}
'Algorithm > 문제풀이' 카테고리의 다른 글
[프로그래머스] 문자열 압축 (0) | 2022.01.09 |
---|---|
[Leetcode] Find All Numbers Disappeared in an Array ★★★★★ (0) | 2021.06.27 |
[Leetcode] Third Maximum Number (0) | 2021.06.27 |
[Leetcode] Replace Elements with Greatest Element on Right Side (0) | 2021.06.26 |
[Leetcode] Check If N and Its Double Exist ★★ (0) | 2021.06.26 |