Last active
August 12, 2023 20:05
-
-
Save rorrorome/4bf239c8f51186d26e52a379711deb80 to your computer and use it in GitHub Desktop.
가까운 좌표 찾기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 제로베이스 백엔드 스쿨 15기 | |
| * 한새롬 | |
| * Mission 1 깜짝과제 2. 가까운 좌표값 찾기 | |
| * | |
| * 다음 조건에 맞는 프로그램을 작성해 보세요. | |
| * - 나의 좌표 값을 입력 받습니다. (입력은 순서대로 x값, y값을 받습니다.) | |
| * - 이후, 임의의 좌표 값을 입력 받습니다.(역시, x값, y값을 입력 받습니다.) | |
| * - 임의로 입력된 좌표 값 중 동일한 좌표 값을 입력하는 경우는 저장하지 않고 다시 입력하도록 합니다. | |
| * - 입력 받은 값이 10개가 되는 경우, | |
| * 입력 받은 좌표 값과 나의 좌표 값의 거리 중 가장 가까운 좌표 값을 화면에 출력하는 프로그램을 작성해 보세요. | |
| */ | |
| import java.awt.Point; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Scanner; | |
| public class Mini0102 { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| int myx = sc.nextInt(); | |
| int myy = sc.nextInt(); | |
| Point me = new Point(myx,myy); | |
| List<Point> xy = new ArrayList<>(); | |
| while(xy.size()<10) { | |
| int xi = sc.nextInt(); | |
| int yi = sc.nextInt(); | |
| Point p = new Point(xi,yi); | |
| if(!xy.contains(p)){ | |
| xy.add(p); | |
| } | |
| } | |
| double[] dis = new double[10]; | |
| for(int i = 0; i<10; i++) { | |
| dis[i] = xy.get(i).distance(myx, myy); | |
| } | |
| double min = dis[0]; | |
| int ans = 0; | |
| for(int i = 0; i<10; i++) { | |
| if(min>=dis[i]) { | |
| min = dis[i]; | |
| ans = i; | |
| } | |
| } | |
| Point answer = xy.get(ans); | |
| int ansx = (int) answer.getX(); | |
| int ansy = (int) answer.getY(); | |
| System.out.println("("+ansx+", "+ansy+")"); | |
| sc.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment