백준 알고리즘/Lang-C | C++

[백준/C] 14173번 Square Pasture

Jongung 2021. 9. 14. 23:36

 

백준 온라인 저지 / 14173번 Square Pasture

https://www.acmicpc.net/problem/14173

 

14173번: Square Pasture

In the example above, the first original rectangle has corners (6,6) and (8,8). The second has corners at (1,8) and (4,9). By drawing a square fence of side length 7 with corners (1,6) and (8,13), the original areas can still be enclosed; moreover, this is

www.acmicpc.net

 

  • 사용언어 : C (C99)
  • 알고리즘 : 수학, 사칙연산

 

설명

 

C 코드

1. 문제 정리

총 8개의 입력을 받아 계산 하는 문제이다.

어렵게 생각 할 것 없이 max와 min을 함수로 만들어 준 뒤 쉽게 문제를 풀었다. 

max와 min 함수는 다른 문제에서도 많이 사용했으니 따로 설명 하지 않겠다.

 

 

 

2. 완성 코드

#include <stdio.h>

int max(int a, int b){
  if(a>=b){
    return a;
  }
  else{
    return b;
  }
}

int min(int a, int b){
  if(a<b){
    return a;
  }
  else{
    return b;
  }
}
int main(void) {
  int x1, y1, x2, y2;
  int x3, y3, x4, y4;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  scanf("%d %d %d %d", &x3, &y3, &x4, &y4);

  int X = max(x2, x4) - min(x1, x3);
  int Y = max(y2, y4) - min(y1, y3);
  int A = max(X,Y);

  printf("%d", A * A);
  return 0;
}