✏️ 문제풀이/백준

[백준/Java] 3009번 :: 네 번째 점

bono-hye 2024. 5. 4. 21:53

| 문제

세 점이 주어졌을 때, 축에 평행한 직사각형을 만들기 위해서 필요한 네 번째 점을 찾는 프로그램을 작성하시오.

 

| 입력

세 점의 좌표가 한 줄에 하나씩 주어진다. 좌표는 1보다 크거나 같고, 1000보다 작거나 같은 정수이다.

 

| 출력

직사각형의 네 번째 점의 좌표를 출력한다.

 

| 예제 입력

5 5
5 7
7 5

 

| 예제 출력

7 7

 

| 풀이 - BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main
{
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		int x1 = Integer.parseInt(st.nextToken());
		int y1 = Integer.parseInt(st.nextToken());
		
		st = new StringTokenizer(br.readLine());
		int x2 = Integer.parseInt(st.nextToken());
		int y2 = Integer.parseInt(st.nextToken());
		
		st = new StringTokenizer(br.readLine());
		int x3 = Integer.parseInt(st.nextToken());
		int y3 = Integer.parseInt(st.nextToken());
		
		int x4, y4;
		
		if(x1 == x2)
			x4 = x3;
		else if(x1 == x3)
			x4 = x2;
		else
			x4 = x1;
		
		if(y1==y2)
			y4 = y3;
		else if(y1==y3)
			y4 = y2;
		else
			y4 = y1;
		
		System.out.println(x4 + " " + y4);
	}
}

| 풀이 - Scanner

import java.util.Scanner;


public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int x1 = sc.nextInt();
		int y1 = sc.nextInt();
		
		int x2 = sc.nextInt();
		int y2 = sc.nextInt();
		
		int x3 = sc.nextInt();
		int y3 = sc.nextInt();
		
		int x4, y4;
		
		if(x1 == x2)
			x4 = x3;
		else if(x1 == x3)
			x4 = x2;
		else
			x4 = x1;
		
		if(y1==y2)
			y4 = y3;
		else if(y1==y3)
			y4 = y2;
		else
			y4 = y1;
		
		sc.close();
		
		System.out.println(x4 + " " + y4);			
	}
}

| 정리

예제 입력을 보다 보니 x값이랑 y값이 두개씩 짝을 이룬다는 공통점이 보였다.

그래서 if 문으로 각각 주어진 값을 비교하고 짝을 이루는 값이 없는 것을 x4, y4 에 값을 넣어줬다!