Vienna
깜짝과제2) 거리가 가장 가까운 좌표값 찾는 프로그램 작성하기 본문
◇ 문제
다음 조건에 맞는 프로그램을 작성해 보세요.
나의 좌표 값을 입력 받습니다. (입력은 순서대로 x값, y값을 받습니다.) - 이후, 임의의 좌표 값을 입력 받습니다.(역시, x값, y값을 입력 받습니다.) - 임의로 입력된 좌표 값 중 동일한 좌표 값을 입력하는 경우는 저장하지 않고 다시 입력하도록 합니다. - 입력 받은 값이 10개가 되는 경우, 입력 받은 좌표 값과 나의 좌표 값의 거리 중 가장 가까운 좌표 값을 화면에 출력하는 프로그램을 작성해 보세요.
예를 들어, 나의 좌표 값이 1, 1 이고 임의로 입력받은 좌표 값들이 (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11) 일 때 나와 가장 가까운 좌표값은 (2, 2) 입니다.
◆ 문제 풀이
피타고라스의 정리를 이용하면 금방 해결할 수 있는 문제다.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("내 좌표 x값을 입력해 주세요.");
int myX = Integer.parseInt(br.readLine());
System.out.println("내 좌표 y값을 입력해 주세요.");
int myY = Integer.parseInt(br.readLine());
Point me = new Point(myX, myY);
Point[] others = new Point[10];
for(int i=0, iMax =others.length; i<iMax;){
System.out.println((i+1)+"/10 번째 입력");
System.out.println("임의의 좌표 x값을 입력해 주세요.");
int x = Integer.parseInt(br.readLine());
System.out.println("임의의 좌표 y값을 입력해 주세요.");
int y = Integer.parseInt(br.readLine());
if(x == myX && y == myY){
System.out.println("내 좌표와 동일합니다. 다시 입력해 주세요.");
continue;
}
boolean exitsted = false;
for(int j=0; j<i; j++){
if(x == others[j].x && y == others[j].y){
System.out.println("동일한 좌표값이 이미 존재합니다. 다시 입력해 주세요.");
exitsted = true;
break;
}
}
if(exitsted)
continue;
others[i++] = new Point(x, y);
}
Point shortest = null;
for(int i=0, iMax = others.length; i<iMax; i++){
if(shortest ==null || others[i].distance(me) < shortest.distance(me)){
shortest = others[i];
}
System.out.printf("(%d, %d) => %6f\n", others[i].x, others[i].y, others[i].distance(me));
}
System.out.println("제일 가까운 좌표:");
System.out.printf("(%d, %d) => %6f\n", shortest.x, shortest.y, shortest.distance(me));
}
static class Point{
int x;
int y;
private double distance;
public Point(int x, int y){
this.x = x;
this.y = y;
}
public double distance(Point point){
if(distance !=0)
return distance;
int diffX = Math.abs(point.x - x);
int diffY = Math.abs(point.y - y);
distance = Math.sqrt(diffX*diffX + diffY * diffY);
return distance;
}
}
}
그리고 다른 분의 풀이를 보고 아이디어가 떠올라서 수정해보았다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("내 좌표 x값을 입력해 주세요.");
int myX = Integer.parseInt(br.readLine());
System.out.println("내 좌표 y값을 입력해 주세요.");
int myY = Integer.parseInt(br.readLine());
Point me = new Point(myX, myY);
LinkedHashSet<Point> set = new LinkedHashSet();
for(int i=0; i<10;){
System.out.println((i+1)+"/10 번째 입력");
System.out.println("임의의 좌표 x값을 입력해 주세요.");
int x = Integer.parseInt(br.readLine());
System.out.println("임의의 좌표 y값을 입력해 주세요.");
int y = Integer.parseInt(br.readLine());
Point point = new Point(x, y);
if(point.equals(me)){
System.out.println("내 좌표와 동일합니다. 다시 입력해 주세요.");
continue;
}
boolean added = set.add(point);
if(added){
i++;
}
else{
System.out.println("동일한 좌표값이 이미 존재합니다. 다시 입력해 주세요.");
}
}
Point shortest = null;
Iterator<Point> it= set.iterator();
while(it.hasNext()){
Point point = it.next();
if(shortest ==null || point.distance(me) < shortest.distance(me)){
shortest = point;
}
System.out.printf("(%d, %d) => %6f\n", point.x, point.y, point.distance(me));
}
System.out.println("제일 가까운 좌표:");
System.out.printf("(%d, %d) => %6f\n", shortest.x, shortest.y, shortest.distance(me));
}
static class Point{
int x;
int y;
private double distance;
public Point(int x, int y){
this.x = x;
this.y = y;
}
public double distance(Point point){
if(distance !=0)
return distance;
int diffX = Math.abs(point.x - x);
int diffY = Math.abs(point.y - y);
distance = Math.sqrt(diffX*diffX + diffY * diffY);
return distance;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || getClass() != obj.getClass()) return false;
Point point = (Point)obj;
return point.x == x && point.y ==y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
}
'그외' 카테고리의 다른 글
알고리즘 복습) 버블 정렬, 삽입 정렬, 선택 정렬 (1) | 2023.06.13 |
---|---|
깜짝과제3) 웹페이지 페이징 처리 (0) | 2023.06.06 |
깜짝과제1) 자바로 hmtl 파일 생성 (0) | 2023.06.06 |
Comments