https://www.hackerrank.com/challenges/icecream-parlor/problem
Ice Cream Parlor | HackerRank
Help Sunny and Johnny spend all their money during each trip to the Ice Cream Parlor.
www.hackerrank.com
<문제>

m은 우리가 쓰고 싶은 돈의 크기이고, cost[]배열은 아이스크림 종류별로의 비용이다.
cost 배열에서 두가지 아이스크림의 값을 합쳤을 때, m과 같게 되는 아이스크림의 순서를 출력하는 문제이다.
<코드>
int* icecreamParlor(int m, int arr_count, int* arr, int* result_count) {
*result_count = 2;
int* a = malloc((*result_count) * sizeof(int)), c = 0;
for (int i = 0; i < arr_count; i++) {
for (int j = i + 1; j < arr_count; j++) {
if (arr[i] + arr[j] == m) {
a[c++] = i + 1;
a[c] = j + 1;
break;
}
}
}
return a;
}
<코드 설명>
먼저 크기 2 만큼의 a배열을 동적할당하고, 중첩된 for문을 이용하여 i값과 j값을 변화시킨다.
arr[i]+arr[j]의 값이 원하는 m값이 되면 a배열에 추가하고 for문을 탈출한다.
a배열을 리턴한다.
<결과>

'c언어 스터디 > HackerRank' 카테고리의 다른 글
| [HackerRank] Staircase (0) | 2021.05.13 |
|---|---|
| [HackerRank] Birthday Cake Candles (0) | 2021.05.07 |
| [HackerRank] Boxes through a Tunnel (0) | 2021.04.07 |
| [HackerRank] Sorting-Correctness and the Loop Invariant (0) | 2021.04.07 |
| [HackerRank] Divisible Sum Pairs (0) | 2021.04.03 |