https://www.hackerrank.com/challenges/too-high-boxes/problem
Boxes through a Tunnel | HackerRank
Find the volume of short enough boxes.
www.hackerrank.com
<문제>

높이가 41인 터널이 있다. 이 터널을 통해 상자들을 보내려고 한다. input에 상자의 밑변 가로, 밑변 세로, 높이를 입력할 때, 터널을 통과할 수 있는 상자들의 부피를 화면에 출력한다.
<코드>
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41
struct box
{
int length;
int width;
int height;
};
typedef struct box box;
int get_volume(box b) {
return b.length*b.width*b.height;
}
int is_lower_than_max_height(box b) {
if (b.height<MAX_HEIGHT){
return 1;
}
else {
return 0;
}
}
int main()
{
int n;
scanf("%d", &n);
box *boxes = malloc(n * sizeof(box));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
}
for (int i = 0; i < n; i++) {
if (is_lower_than_max_height(boxes[i])) {
printf("%d\n", get_volume(boxes[i]));
}
}
return 0;
}
<풀이>
1. struct box로 상자의 밑변의 가로 세로와 높이를 저장한다. typedef를 이용해 struct box를 box로 지정한다.
2. get_volume()함수를 이용해 상자의 부피를 구한다.
3. is_lower_than_max_height() 함수를 이용해 상자의 높이가 터널의 높이보다 크면 0, 낮으면 1을 리턴한다.
4. main()에서 터널로 보낼 상자의 개수 n을 입력받고, n만큼 box배열을 만든다.
5. n개 상자의 가로, 세로, 높이 길이를 입력받고 배열에 저장한다.
6. 만약 is_lower_than_max_height() 함수의 값이 참이면 상자의 부피 값을 화면에 출력하고, 거짓이면 아무것도 출력하지 않는다.
'c언어 스터디 > HackerRank' 카테고리의 다른 글
| [HackerRank] Birthday Cake Candles (0) | 2021.05.07 |
|---|---|
| [HackerRank] Search- IceCream Parlor (0) | 2021.05.07 |
| [HackerRank] Sorting-Correctness and the Loop Invariant (0) | 2021.04.07 |
| [HackerRank] Divisible Sum Pairs (0) | 2021.04.03 |
| [HackerRank] Insertion Sort-Part2 (0) | 2021.04.03 |