모두의 dream
백준 1085 본문
Contents
접기
1085번: 직사각형에서 탈출 (acmicpc.net)
#include <stdio.h>
int main()
{
int x = 0, y = 0, w = 0, h = 0;
int result1 = 0, result2 = 0;
scanf("%d %d %d %d", &x, &y, &w, &h);
result1 = x;
if (result1 > w-x)
result1 = w-x;
result2 = y;
if (result2 > h - y)
result2 = h - y;
printf("%d\n", result1 < result2 ? result1 : result2);
return 0;
}
result1에서 x와 0, w 사이의 최소값을 찾아내고 result2에서 y와 0, h 사이의 최소값을 찾아내서 최종적으로 result1과 result2 중에서 더 작은 값을 최소값으로 정해서 출력하는 방식으로 코딩함.
구글링을 하다보니 아차 싶었던 게 아래처럼 코드를 짜다가 위 코드로 변경했는데...
#include <stdio.h>
int main()
{
int x = 0, y = 0, w = 0, h = 0;
int result = 0;
scanf("%d %d %d %d", &x, &y, &w, &h);
result = x;
if (result > w-x)
result = w-x;
if (result > y)
result = y;
if (result > h - y)
result = h - y;
printf("%d\n", result);
return 0;
}
어짜피 최소값 구하는거라 if문을 지나온 이상 아래 연산보다 더 큰 수는 나올 수 없는데 잠깐 착각해서 더 큰 수가 나오면 어떡하지라는 고민을 했다;;
정신 단단히 무장 필요.
Comments