Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Lv.1/Lv.1 로또의 최고 순위와 최저 순위.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

// 순위를 알려주는 함수
int get_rank(int corr) {
switch (corr) {
case 0: return 6;
case 1: return 6;
case 2: return 5;
case 3: return 4;
case 4: return 3;
case 5: return 2;
case 6: return 1;
}
}

vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer;
// 0(알아볼 수 없는 수)의 개수
int cnt_0 = 0;
// 로또 당첨 번호와 일치하는 개수
int corr = 0;

// 오름차순으로 정렬
sort(lottos.begin(), lottos.end());
sort(win_nums.begin(), win_nums.end());

for (int i = 0, j = 0; i < 6; i++) {
// 알아볼 수 없는 수의 개수 세기
if (lottos[i] == 0) {
cnt_0++;
continue;
}


// 구매한 번호가 작으면 i를 추가하고
// 당첨번호가 작으면 j를 추가해서 비교해야 함
// 구매한 번호가 당첨 번호보다 크거나 같을 때
while (lottos[i] >= win_nums[j]) {
if (lottos[i] == win_nums[j]) {
// 당첨 번호와 일치하면 corr 추가
corr++;
}
// 다음 당첨번호와 비교
j++;
}

}

// 최고 순위 = 알아볼 수 없는 번호가 전부 당첨번호와 일치할 때
answer.push_back(get_rank(corr + cnt_0));
answer.push_back(get_rank(corr));

return answer;
}
29 changes: 29 additions & 0 deletions Lv.2/Lv.2 타겟 넘버.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <string>
#include <vector>

using namespace std;

int answer = 0;

void get_target_number(vector<int> numbers, int target, int sum, int index) {
if (index == numbers.size()) {
// 모든 숫자를 다 사용했을 때 종료
if (sum == target) {
// 만약에 모든 숫자를 다 사용한 결과가 타겟 넘버와 일치하면 answer 추가
answer++;
}
return;
}

// 숫자를 다 사용하지 않았을 경우 다음 더했을 때랑 뺄 때의 경우 모두 탐색
get_target_number(numbers, target, sum + numbers[index], index + 1);
get_target_number(numbers, target, sum - numbers[index], index + 1);


}

int solution(vector<int> numbers, int target) {
get_target_number(numbers, target, 0, 0);

return answer;
}