ARCTIC 오답 도움요청드립니다.

  • scarabcandy
    scarabcandy

    이클립스에서 돌리면 답이 잘 나오는데 답안을 제출하면

    계속 오답이라고 나옵니다. 여러가지 예제입력을 해보고

    코드를 봐도 어디서 잘못된건지 모르겠습니다. 도와주세요.

    import java.util.ArrayList;
    import java.util.Scanner;
    
    class Coordinate {
        double x = 0.0;
        double y = 0.0;
    }
    
    public class Main {
        public static void main(String[] args) {
            int caseNum = 0;
            int stationNum = 0;
            ArrayList<ArrayList<Coordinate>> testCaseList = new ArrayList<ArrayList<Coordinate>>();
            Scanner scan = new Scanner(System.in);
    
            caseNum = scan.nextInt();
    
            for (int i = 0; i < caseNum; i++) {
                ArrayList<Coordinate> stationList = new ArrayList<Coordinate>();
    
                stationNum = scan.nextInt();
    
                for (int j = 0; j < stationNum; j++) {
                    Coordinate coo = new Coordinate();
    
                    coo.x = scan.nextDouble();
                    coo.y = scan.nextDouble();
                    stationList.add(coo);
                }
    
                testCaseList.add(stationList);
            }
    
            for (ArrayList<Coordinate> stationList : testCaseList) {
                ArrayList<Double> closeDistanceList = new ArrayList<Double>();
                double minimumPower = 0.0;
    
                for (int standard = 0; standard < (stationList.size() - 1); standard++) {
                    double closeDistance = 2000000.0;
    
                    for (int comparison = standard + 1; comparison < stationList.size(); comparison++) {
                        double differenceX = stationList.get(standard).x - stationList.get(comparison).x;
                        double differenceY = stationList.get(standard).y - stationList.get(comparison).y;
                        double comparativeDistance = Math.pow(differenceX, 2) + Math.pow(differenceY, 2);
    
                        if (comparativeDistance < closeDistance) {
                            closeDistance = comparativeDistance;
                        }
                    }
    
                    closeDistanceList.add(closeDistance);
                }
    
                for (int i = 0; i < closeDistanceList.size(); i++) {
                    if (minimumPower < closeDistanceList.get(i)) {
                        minimumPower = closeDistanceList.get(i);
                    }
                }
    
                minimumPower = Math.round(Math.sqrt(minimumPower) * 100) / 100.0;
    
                System.out.println();
                System.out.printf("%.2f", minimumPower);
            }
        }
    }
    

    10년 전
4개의 댓글이 있습니다.
  • JongMan
    JongMan

    기지의 모든 쌍 중 가장 먼 거리를 출력하시는 것 맞나요? 그렇다면 알고리즘이 틀렸습니다. 이것이 답이 아닌 예를 쉽게 만드실 수 있을 겁니다.


    10년 전 link
  • scarabcandy
    scarabcandy

    기지별로 돌아가면서 다른 기지와의 거리들 중에서 가장 짧은 것을 배열에 넣도록 반복한 후에 그 배열에서 가장 거리가 먼 것을 출력하도록했습니다.
    예를 들어 5개의 기지를 입력받으면 첫번째 기지에서 나머지 4개 기지로의 거리들 중에서 가장 짧은 것을 배열에 놓고 그 다음에 두번째 기지에서 첫번째 기지를 제외한 나머지 3개 기지로의 거리들 중에서 가장 짧은 것을 배열에 넣는 것을 반복한 후에 배열에 있는 짧은 거리들 중에서 가장 먼 것을 최소출력으로 출력했습니다.


    10년 전 link
  • JongMan
    JongMan

    오 그렇군요. 그렇다고 해도 알고리즘이 틀렸습니다. 해당 값이 답이 아닌 경우를 쉽게 만드실 수 있을 겁니다.


    10년 전 link
  • JongMan
    JongMan

    힌트를 드리자면,


    기지들이 두 묶음으로 나뉘어져, 한 묶음끼리는 서로 가깝고 두 묶음은 멀리 떨어져 있는 경우의 답을 생각해 보세요.


    10년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.