문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/77484
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
간단한 구현 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
int getRank(int sum) | |
{ | |
switch (sum) | |
{ | |
case 6: | |
return 1; | |
case 5: | |
return 2; | |
case 4: | |
return 3; | |
case 3: | |
return 4; | |
case 2: | |
return 5; | |
default: | |
return 6; | |
} | |
} | |
vector<int> solution(vector<int> lottos, vector<int> win_nums) { | |
int match = 0; | |
int zero = 0; | |
for (int i = 0; i < lottos.size(); i++) | |
{ | |
zero += !lottos[i]; | |
for (int j = 0; j < win_nums.size(); j++) | |
{ | |
match += (lottos[i] == win_nums[j]); | |
} | |
} | |
return { getRank(match + zero), getRank(match) }; | |
} | |
int main(void) | |
{ | |
vector<int> lottos = { 44, 1, 0, 0, 31, 25 }; | |
vector<int> win_nums = { 31, 10, 45, 1, 6, 19 }; | |
vector<int> answers = solution(lottos, win_nums); | |
for (int answer : answers) | |
{ | |
cout << answer << " "; | |
} | |
cout << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 모두 0으로 만들기 (0) | 2021.09.29 |
---|---|
[Programmers 위클리 챌린지 8주차] 최소직사각형 (0) | 2021.09.27 |
[Programmers 코딩테스트 고득점 Kit] 징검다리 (0) | 2021.09.26 |
[Programmers 코딩테스트 고득점 Kit] 입국심사 (0) | 2021.09.26 |
[Programmers 코딩테스트 고득점 Kit] 단속카메라 (0) | 2021.09.25 |