최대한 많은 쌍의 장갑 개수 구하기
문제 설명
왼손 장갑의 제품 번호가 들어있는 배열과 오른손 장갑의 제품 번호가 들어있는 배열이 있습니다.
제품 번호는 1 ~ 10 사이의 자연수입니다.
제품 번호가 같은 왼손 장갑과 오른손 장갑을 합쳐 장갑 한 쌍을 만들 수 있습니다.
1. 왼손 장갑이 제품 번호별로 몇 개씩 있는지 개수를 셉니다.
2. 오른손 장갑이 제품 번호별로 몇 개씩 있는지 개수를 셉니다.
3. 각 제품 번호별로 최대한 많은 장갑 쌍을 만들면서 개수를 셉니다.
왼손 장갑의 제품 번호가 들어있는 배열 left_gloves와 left_gloves의 길이 left_gloves_len, 오른손 장갑의 제품 번호가 들어있는 배열 right_gloves와 right_gloves의길이 right_gloves_len이 매개변수로 주어질 때, 최대한 몇개의 장갑 쌍을 만들 수 있는지 return하도록 solution 함수를 작성하려고 합니다. 이때, 위 구조를 참고하여 중복되는 부분은 func_a라는 함수를 작성했습니다. 코드가 올바르게 동작할 수 있도록 빈칸을 채워주세요.
예시
left_gloves | left_gloves_len | right_gloves | right_gloves_len | return |
[2, 1, 2, 2, 4] | 5 | [1, 2, 2, 4, 4, 7] | 6 | 4 |
코드 해석
for문 1) 0 ~ max_product_number까지 : gloves의 원소는 1 ~ 10이하의 자연수라는 조건 때문에 10까지 돌린다.
동적 할당된 배열의 값을 초기화해주는 for문
for문 2) 0 ~ gloves_len - 1 까지 반복하고, counter[gloves[i]] 방을 1 더한다.
left_gloves가 매개변수일 경우
2 | 1 | 2 | 2 | 4 |
counter[2]++ | counter[1]++ | counter[2]++ | counter[2]++ | counter[4]++ |
counter 배열은 [0, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0]가 된다.
right_gloves가 매개변수일 경우
1 | 2 | 2 | 4 | 4 | 7 |
counter[1]++ | counter[2]++ | counter[2]++ | counter[4]++ | counter[4]++ | counter[7]++ |
counter 배열은 [0, 1, 2, 0, 2, 0, 0, 1, 0, 0, 0]가 된다.
int* func_a(int gloves[], int gloves_len) {
int* counter = (int*)malloc(sizeof(int) * (max_product_number + 1));
for (int i = 0; i <= max_product_number; ++i)
counter[i] = 0;
for (int i = 0; i < gloves_len; ++i)
counter[gloves[i]]++;
return counter;
}
1행 : left_counter
2행 : right_counter
3행 mini함수의 return 값
1 | 3 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 2 | 0 | 2 | 0 | 0 | 1 | 0 | 0 | 0 |
1 | 2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
total = 1 + 2 + 1 = 4
int mini(int a, int b) {
return a < b ? a : b;
}
.
.
.
int solution(int left_gloves[], int left_gloves_len, int right_gloves[], int right_gloves_len) {
int* left_counter = func_a(left_gloves, left_gloves_len);
int* right_counter = func_a(right_gloves, right_gloves_len);
int total = 0;
for (int i = 1; i <= max_product_number; ++i)
total += mini(left_counter[i], right_counter[i]);
return total;
}
전체 코드
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
const int max_product_number = 10;
int* func_a(int gloves[], int gloves_len) {
int* counter = (int*)malloc(sizeof(int) * (max_product_number + 1));
for (int i = 0; i <= max_product_number; ++i)
counter[i] = 0;
for (int i = 0; i < gloves_len; ++i)
counter[i]++;
return counter;
}
int mini(int a, int b) {
return a < b ? a : b;
}
int solution(int left_gloves[], int left_gloves_len, int right_gloves[], int right_gloves_len) {
int* left_counter = func_a(left_gloves, left_gloves_len);
int* right_counter = func_a(right_gloves, right_gloves_len);
int total = 0;
for (int i = 1; i <= max_product_number; ++i)
total += mini(left_counter[i], right_counter[i]);
return total;
}
int main() {
int left_gloves[5] = { 2, 1, 2, 2, 4 };
int left_gloves_len = 5;
int right_gloves[6] = { 1, 2, 2, 4, 4, 7 };
int right_gloves_len = 6;
int ret = solution(left_gloves, left_gloves_len, right_gloves, right_gloves_len);
printf("The return value of solution function is %d \n", ret);
}
'코딩 테스트 > [c언어] cos pro 2급 기출문제' 카테고리의 다른 글
[No.13] 짝수들의 제곱의 합 구하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.09 |
---|---|
[No.12] 더 많은 배수 구하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.08 |
[No.10] 평균 이하의 개수 구하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.08 |
[No.9] 중복 문자 삭제하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.07 |
[No.8] 팰린드롬 판단하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.06 |
댓글