[코딩테스트][백준] 3009. 네 번째 점

김상욱·2024년 7월 8일
0

문제

https://www.acmicpc.net/problem/3009

python

x_cnt=dict()
y_cnt=dict()
for _ in range(3):
    x,y=map(int,input().split())
    if x not in x_cnt:
        x_cnt[x]=1
    else:
        x_cnt[x]+=1
    if y not in y_cnt:
        y_cnt[y]=1
    else:
        y_cnt[y]+=1
answer_x=0
answer_y=0
for k in x_cnt.keys():
    if x_cnt[k]==1:
        answer_x=k
for k in y_cnt.keys():
    if y_cnt[k]==1:
        answer_y=k
print(answer_x,answer_y)

java

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Map<Integer,Integer> map_x=new HashMap<Integer,Integer>();
        Map<Integer,Integer> map_y=new HashMap<Integer,Integer>();
        for(int i=0;i<3;i++){
            int x=sc.nextInt();
            int y=sc.nextInt();
            if(map_x.containsKey(x)){
                map_x.put(x,2);
            }else{
                map_x.put(x,1);
            }
            if(map_y.containsKey(y)){
                map_y.put(y,2);
            }else{
                map_y.put(y,1);
            }
        }
        int answer_x=0;
        int answer_y=0;
        for(int x : map_x.keySet()){
            if(map_x.get(x)==1){
                answer_x=x;
            }
        }
        for(int y : map_y.keySet()){
            if(map_y.get(y)==1){
                answer_y=y;
            }
        }
        System.out.printf("%d %d",answer_x,answer_y);
    }
}

내 생각

  • 풀이시간 15분
  • 자바에서 map의 사용법이 기억이 안나서 조금 시간이 걸렸다. 코드 자체는 각 x,y로 나누어서 같은 값이 몇개나 나오는지 map에 기록하고 그 기록이 한개 인 것을 찾으면 사각형의 남은 한 점의 좌표가 된다.

0개의 댓글