아하
검색 이미지
생활꿀팁 이미지
생활꿀팁생활
생활꿀팁 이미지
생활꿀팁생활
아리따운멋돼지33
아리따운멋돼지3324.03.26

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;

}

55글자 더 채워주세요.
답변의 개수1개의 답변이 있어요!
  • 안녕하세요. 태평한토끼127입니다.

    사각형 1과 사각형 2가 겹치는 부분의 넓이를 구하는 문제를 해결하려면, 두 사각형이 겹치는지 확인하고, 겹치는 경우에만 겹치는 부분의 넓이를 계산해야 합니다.

    두 사각형이 겹치는지 확인하는 방법은 다음과 같습니다:

    사각형 1의 오른쪽 아래 꼭짓점이 사각형 2의 왼쪽 위 꼭짓점보다 오른쪽이거나 아래에 있어야 합니다.

    사각형 1의 왼쪽 위 꼭짓점이 사각형 2의 오른쪽 아래 꼭짓점보다 왼쪽이거나 위에 있어야 합니다.

    이 조건을 만족하는 경우에만 두 사각형은 겹칩니다. 이 경우에는 겹치는 부분의 넓이를 계산할 수 있습니다. 겹치는 부분의 넓이는 겹치는 부분의 가로 길이와 세로 길이를 곱하여 구할 수 있습니다.

    아래는 이를 구현한 코드입니다:

    #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 < right_up_x && right_down_x > left_down_x && left_up_y < right_up_y && right_down_y > left_down_y) {

    ans_x = (right_down_x < right_up_x ? right_down_x : right_up_x) - (left_up_x > left_down_x ? left_up_x : left_down_x);

    ans_y = (left_up_y > left_down_y ? left_up_y : left_down_y) - (right_down_y < right_up_y ? right_down_y : right_up_y);

    result = ans_x * ans_y;

    printf("%d", result);

    } else {

    printf("0");

    }

    return 0;

    }

    이 코드는 두 사각형이 겹치는 경우에만 겹치는 부분의 넓이를 계산하고 출력합니다. 두 사각형이 겹치지 않는 경우에는 0을 출력합니다. 이 코드는 주어진 조건문만을 사용하여 작성되었습니다. 이 코드가 도움이 되었기를 바랍니다