Algorithm

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

1984 2022. 11. 17. 17:15

* x, y 좌표가 2개씩 같아야 함.

* HashMap 사용 - key값에 좌표 값, value 값이 빈도 수 입력

* 람다식 사용해서 HashMap foreach

import java.io.*;
import java.util.*;

public class Main {

	public static class Point {
		int x;
		int y;

		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		Map<Integer, Integer> xMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> yMap = new HashMap<Integer, Integer>();

		for (int i = 0; i < 3; i++) {
			StringTokenizer stk = new StringTokenizer(br.readLine(), " ");
			int x = Integer.parseInt(stk.nextToken());
			int y = Integer.parseInt(stk.nextToken());

			if (xMap.containsKey(x)) {
				xMap.put(x, xMap.get(x) + 1);
			} else {
				xMap.put(x, 1);
			}

			if (yMap.containsKey(y)) {
				yMap.put(y, yMap.get(y) + 1);
			} else {
				yMap.put(y, 1);
			}
		}

		Point p = new Point(0, 0);
		xMap.forEach((key, value) -> {
			if (value == 1) {
				p.x = key;
			}
		});
		yMap.forEach((key, value) -> {
			if (value == 1) {
				p.y = key;
			}
		});

		System.out.println(p.x + " " + p.y);

	}

}
728x90