Skip to content

Instantly share code, notes, and snippets.

@YongWanJin
Created August 7, 2023 11:57
Show Gist options
  • Save YongWanJin/38dd57fcb74dba89fc23de0c6a75144c to your computer and use it in GitHub Desktop.
Save YongWanJin/38dd57fcb74dba89fc23de0c6a75144c to your computer and use it in GitHub Desktop.
2023.08.07. Mission1 "깜짝과제" 2번
import java.util.*;
import java.io.*;
public class Mission1Surprise2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 나의 좌표 값 입력받기 (공백으로 x값, y값 구분)
int[] myPlace = new int[2];
StringTokenizer st = new StringTokenizer(br.readLine());
myPlace[0] = Integer.parseInt(st.nextToken());
myPlace[1] = Integer.parseInt(st.nextToken());
// 임의의 좌표 값 10개 입력받기 (공백으로 x값과 y값 구분, 개행문자로 좌표 구분)
int[][] places = new int[10][2];
int i = 0;
while( i < 10 ) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
// 중복 입력값 판별
boolean isDuplicated = false;
for(int k = 0; k < i; k++){
if( places[k][0] == x && places[k][1] == y ){
isDuplicated = true;
break;
}
}
// 중복 입력값 저장 안하기, 중복 아닌 경우 저장
if(isDuplicated){
continue;
} else {
places[i][0] = x;
places[i][1] = y;
i++;
}
}
// 나의 좌표와 가장 가까운 좌표 판별
int min = Integer.MAX_VALUE;
int minIdx = 0;
for (int j = 0; j < 10; j++) {
int dist = Math.abs(myPlace[0]-places[j][0]) + Math.abs(myPlace[1]-places[j][1]);
if(dist < min){
min = dist;
minIdx = j;
}
}
// 결과 출력
String result = "(" + places[minIdx][0] + ", " + places[minIdx][1] + ")";
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment