happy coding
[java] 1085. 직사각형에서 탈출 본문
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
//첫째 줄에 공백으로 구분된 x,y,w,h를 입력 받음
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
int output_height = 0;
int output_width = 0;
//w,h을 이용해서 현재위치인 (x,y)에서 탈출할 수 있는 거리의 최솟값 출력
if (y<h) {
output_height = h-y;
System.out.println("세로 : " + output_height);
}
if (x<w) {
output_width = w-x;
System.out.println("가로 : " + output_width);
}
if (output_height > output_width) {
if (output_width > x) {
System.out.println(x);
} else {
System.out.println(output_width);
}
} else if (output_height < output_width) {
if (output_height > y) {
System.out.println(y);
} else {
System.out.println(output_height);
}
} else {
System.out.println(output_width);
}
}
}
처음엔 노가다로 했는데 문제는, 테케2에 안맞아도 변수가 너무 많아서 머리가 아팠다. 그래서 코드를 수정. 함수씀
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
//첫째 줄에 공백으로 구분된 x,y,w,h를 입력 받음
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
//w,h을 이용해서 현재위치인 (x,y)에서 탈출할 수 있는 거리의 최솟값 출력
int minDistance = Math.min(Math.min(x, w - x), Math.min(y, h - y));
System.out.println(minDistance);
}
}
아 함수 편하다..C처럼 하나 하나 안해도 되는구낭..
'coding study > baekjoon' 카테고리의 다른 글
[java] 23037. 5의 수난 (0) | 2023.07.28 |
---|---|
[java] 28062. 준석이의 사탕 사기 (0) | 2023.07.28 |
[java] 1284. 집주소 (0) | 2023.07.27 |
[java] 2501. 약수 구하기 (0) | 2023.07.26 |
[java] 10817. 세 수 (0) | 2023.07.25 |
Comments