c언어 어떻게 해결할 수 있을까요??
사각형 1 : 왼쪽 위 꼭짓점 좌표, 오른쪽 아래 꼭짓점 좌표
사각형 2 : 오른쪽 위 꼭짓점 좌표, 왼쪽 아래 꼭짓점 좌표
를 입력하고 겹치는 부분의 넓이를 정수로 출력, 겹치지 않을 경우에는 0으로 출력해야 합니다
조건문까지만 배웠다고 가정했을 때 코드를 어떻게 완성할 수 있을까요?
#include <stdio.h>
int main() {
int left_up_x, left_up_y, right_down_x, right_down_y, left_down_x, left_down_y, right_up_x, right_up_y;
int ans_x, ans_y, result;
scanf("%d %d", &left_up_x, &left_up_y);
scanf("%d %d", &right_down_x, &right_down_y);
scanf("%d %d", &left_down_x, &left_down_y);
scanf("%d %d", &right_up_x, &right_up_y);
if (left_up_x < left_down_x && right_down_x > left_down_x && right_up_y > right_down_y) // 문제 그림
{
ans_x = right_down_x - left_down_x;
ans_y = right_up_y - right_down_y;
result = ans_x * ans_y;
printf("%d", result);
}
else if (left_up_x > left_down_x && right_up_x > left_up_x && right_up_y > right_down_y) // 좌우반전
{
ans_x = right_up_x - left_up_x;
ans_y = right_up_y - right_down_y;
result = ans_x * ans_y;
printf("%d", result);
}
else if (left_up_x > left_down_x && right_up_x > left_up_x && left_up_y > left_down_y)
{
ans_x = right_up_x - left_up_x;
ans_y = left_up_x - left_down_y;
result = ans_x * ans_y;
printf("%d", result);
}
else if (left_up_x < left_down_x && right_down_x > left_down_x && left_up_y > left_down_y)
{
ans_x = right_down_x - left_down_x;
ans_y = left_up_y - left_down_y;
result = ans_x * ans_y;
printf("%d", result);
}
else
printf("0");
return 0;
}