Created
December 15, 2023 13:22
-
-
Save ramyo564/de5ce7de3f0925964bf18f8e8e994d9e to your computer and use it in GitHub Desktop.
This file contains 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.HashSet; | |
import java.util.Objects; | |
import java.util.Scanner; | |
import java.util.Set; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
// 나의 좌표 값 | |
System.out.println("나의 좌표 값을 입력하세요."); | |
Coordinate myCoordinate = getCoordinate(); | |
// 입력 받은 좌표 값 중 나와 가장 가까운 좌표 | |
Coordinate closestCoordinate = null; | |
double minDistance = Double.MAX_VALUE; | |
System.out.println("타인이 임의의 좌표 값을 10번 입력하세요."); | |
Set<Coordinate> coordinateSet = new HashSet<>(); | |
for (int i = 0; i < 10; i++) { | |
System.out.println("좌표 #" + (i + 1)); | |
Coordinate currentCoordinate = getCoordinate(); | |
coordinateSet.add(currentCoordinate); | |
} | |
for (Coordinate currentCoordinate : coordinateSet) { | |
double distance = calculateDistance(myCoordinate, currentCoordinate); | |
if (distance < minDistance) { | |
minDistance = distance; | |
closestCoordinate = currentCoordinate; | |
} | |
} | |
System.out.println("나와 가장 가까운 좌표: " + closestCoordinate); | |
} | |
private static Coordinate getCoordinate() { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("X 좌표: "); | |
int x = scanner.nextInt(); | |
System.out.print("Y 좌표: "); | |
int y = scanner.nextInt(); | |
return new Coordinate(x, y); | |
} | |
private static double calculateDistance(Coordinate c1, Coordinate c2) { | |
return Math.sqrt(Math.pow(c2.getX() - c1.getX(), 2) + Math.pow(c2.getY() - c1.getY(), 2)); | |
} | |
} | |
class Coordinate { | |
private int x; | |
private int y; | |
public Coordinate(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
@Override | |
public String toString() { | |
return "(" + x + ", " + y + ")"; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null || getClass() != obj.getClass()) return false; | |
Coordinate that = (Coordinate) obj; | |
return x == that.x && y == that.y; | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(x, y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment