프로그래머스 C++ 96

[C/C++] programmers Level 3 최고의 집합

코드 #include using namespace std; vector solution(int n, int s) { if(n>s){ // 자연수 n개를 가지고는 합이 s인 집합을 만들 수 없는 경우 return {-1}; } // 곱이 최대한인 집합을 만드는 문제 이기 때문에 집합의 요소들이 균등해야 합니다. vector answer(n,s/n); // s/n을 통해 균등하데 값을 넣음 int num=s%n; // 나머지값 for(int i=n-1;num>0;i--){ // 값은 오른차순 정렬이기에 뒤에서부터 나머지 값을 더해주며 균등하게 만듬 answer[i]++; num--; } return answer; }

[C/C++] programmers Level 1 로또의 최고 순위와 최저 순위

코드 #include using namespace std; int ranking(int count) { // 일치하는 번호 개수 별 순위 배정 switch (count) { case 6: return 1; case 5: return 2; case 4: return 3; case 3: return 4; case 2: return 5; default: return 6; } } vector solution(vector lottos, vector win_nums) { vector answer; vector num(46,0); // 0 ~ 46 범위 0으로 채움 int count=0; for(int i=0;i i번째 번호 값 증가 num[win_nums[i]]++; // win_nums -> i번째 번호 값 증가..

[C/C++] programmers Level 3 이중우선순위큐

코드 #include #include #include // 큐 #include using namespace std; vector solution(vector operations) { vector answer; priority_queue p_q1; // 내림차순 우선순위 큐 priority_queue p_q2; // 오름차순 우선순위 큐 char command; int num; int size=0; // 전체 큐 크기 for(int i=0;i>command; // 명령어 ss>>num; // 숫자 if(command=='I'){ p_q1.push(num); // 최댓값 기준 p_q2.push(num); // 최솟값 기준 size++; }else{ if(num==1 && size>0){ // D 1 -> 큐..

[C/C++] programmers Level 2 타겟 넘버 - 참고

코드 #include using namespace std; int answer = 0; // 전역 변수 void DFS(vector numbers, int target, int sum=0, int index=0){ if (index == numbers.size()){ // 종료 조건 if (sum == target) { answer++; } return; } // 반복적 탐색 DFS(numbers, target, sum + numbers[index], index + 1); // 더해주는 경우 DFS(numbers, target, sum - numbers[index], index + 1); // 빼주는 경우 } int solution(vector numbers, int target) { DFS(number..

[C/C++] programmers Level 2 의상

코드 #include #include #include // 해시 using namespace std; int solution(vector clothes) { int answer = 1; unordered_map hashmap; for(int i=0;i headgear 1 착용, headgear 2 착용, 착용 X -> 경우의 수 3 (총 개수(2) + 착용하지 않은 경우(1)) // ex) eyewear 총 개수 1 일 경우 -> eyewear 1 착용, 착용 X -> 경우의 수 2 // 3*2=6 -> 서로 다른 조합의 수 return answer-1; // -1 -> 아무것도 착용하지 않은 경우는 빼준다 }

[C/C++] programmers Level 0 주사위 게임 3

코드 #include // 해시 #include // min using namespace std; int solution(int a, int b, int c, int d) { int answer = 0; unordered_map hashmap; // 주사위 굴려 나온 숫자별 횟수 카운트 hashmap[a]++; hashmap[b]++; hashmap[c]++; hashmap[d]++; for(auto n1:hashmap){ if(n1.second==4){ // 네 주사위에서 나온 숫자가 모두 같을 경우 answer=n1.first*1111; } if(n1.second==3){ // 세 주사위에서 나온 숫자가 n1.first로 같고 나머지 다른 주사위에서 나온 숫자가 n2.first for(auto n2:..