Created
July 12, 2023 07:38
-
-
Save dkwktm45/431d242924bcc13219bcdfaeca3c687e 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
| import java.util.*; | |
| public class mission2 { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| System.out.print("x 좌표를 입력 : "); | |
| int x = sc.nextInt(); | |
| System.out.print("y 좌표를 입력 : "); | |
| int y = sc.nextInt(); | |
| List<int[]> xyList = new ArrayList<>(); | |
| randomXY(xyList, sc); | |
| int min = Integer.MAX_VALUE; | |
| int[] result = new int[2]; | |
| for (int[] xy : xyList) { | |
| int value = Math.abs(xy[0] - x)^2 + Math.abs(xy[1] - y)^2; | |
| if (min > value) { | |
| min = value; | |
| result[0] = xy[0]; | |
| result[1] = xy[1]; | |
| } | |
| } | |
| System.out.println(String.format("(%d, %d)", result[0],result[1])); | |
| sc.close(); | |
| } | |
| public static void randomXY(List<int[]> xyList, Scanner sc) { | |
| while (xyList.size() < 10) { | |
| System.out.print(xyList.size() + 1 + "번째 임의의 x 좌표를 입력 : "); | |
| int rdX = sc.nextInt(); | |
| System.out.print(xyList.size() + 1 + "번째 임의의 y 좌표를 입력 : "); | |
| int rdY = sc.nextInt(); | |
| boolean flag = true; | |
| for (int[] xy : xyList) { | |
| if (xy[0] == rdX && xy[1] == rdY) { | |
| System.out.println("나의 좌표값과는 다른값이 입력되어야 합니다."); | |
| flag = false; | |
| break; | |
| } | |
| } | |
| if (flag) xyList.add(new int[]{rdX, rdY}); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment