Algorithm

[백준] 3009번 네 번째 점 (JAVA)

KMSEOP 2020. 5. 21. 20:50
728x90

문제

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

 

입력

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

 

출력

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

 

예제 입력1

30 20

10 10

10 20

 

예제 출력1

30 10

 

코드

import java.util.Scanner;

public class no_3009 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int[] x = new int[3];
		int[] y = new int[3];
		int[] result = new int[2];
		int res = 0;
		
		for(int i=0; i<x.length; i++) {
			x[i] = sc.nextInt();
			y[i] = sc.nextInt();
		}
		
		loop :
		for(int i=0; i<x.length; i++) {
			for(int j=0; j<x.length; j++) {
				if(x[i] == x[j] && i != j) {
					continue loop;
				}
			}
			result[0] = x[i];
		}
		
		loop :
		for(int i=0; i<y.length; i++) {
			for(int j=0; j<y.length; j++) {
				if(y[i] == y[j] && i != j) {
					continue loop;
				}
			}
			result[1] = y[i];
		}
		
		System.out.println(result[0] + " " + result[1]);
	}
}

 

모범답안

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] x = new int[3];
		int[] y = new int[3];
		int dx, dy;
		
		for(int i = 0; i < 3; i++) {
			x[i] = sc.nextInt();
			y[i] = sc.nextInt();
		}
		
		if(x[1] == x[2])
			dx = x[0];
		else
			dx = (x[0] == x[1]) ? x[2] : x[1];
		
		if(y[1] == y[2])
			dy = y[0];
		else
			dy = (y[0] == y[1]) ? y[2] : y[1];
			
		System.out.println(dx + " " + dy);
	}
}

 

728x90