2016-10-19 11 views
-3

나는 숙제를 할당 받았고 완전히 멈췄다 (레벨 : 초급).배열의 한 점에서 3 개의 가장 가까운 좌표를 찾는 방법 Java

사용자 항목과 배열의 모든 점에서 3 개의 가장 가까운 거리를 찾는 방법을 만들어야합니다. 여기에 붙어 있습니다.

방법은 : 공용 static INT [] troisPlusProches 을 INT (X를, INT의 예는, [] coordonneesHabitations를 INT) 여기서 INT X 및 Y는 사용자 항목이다 INT, 상기 어레이 INT [] coordonneesHabitations 값 int [] coordonneesHabitations 인 = {9, 30, 18, 8, 3, 18, 25, 36}. 포인트는 (9,30), (18,8), (3,18) 및 (25,36)입니다.

거리를 계산하기 위해 distance = Math.sqrt (((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))) 공식을 사용했습니다.

이제 사용자 항목에서 3 개의 최단 거리를 찾고 새 배열에서 위치를 반환해야합니다.

사용자 항목이 x = 10, y = 15 인 경우.

가장 짧은 거리는 점 (3, 18)에서 7.616이고 다음 점은 점 (18, 8)에서 10.630이고 세 번째 점은 점 (9, 30)에서 15.033입니다. 이 경우 메서드는 int [] troisPlusProches = {3, 18, 18, 8, 9, 30} 배열을 반환해야합니다. 내가해야 할 일을 알고

, 난 그냥 ... 어떻게

여기에 많은 잘못된 시도 중 하나 알아낼 수 없습니다 :

public static int[] troisPlusProches (int x, int y, int[] (coordonneesHabitations) 
{ 
    int [] that = Arrays.copyOf(coordonneesHabitations, coordonneesHabitations.length); 
    int table[] = new int[6]; 
    double distanceA = 0.0; 
    double minDistance = Float.MAX_VALUE; 
    int a = 0; 
    int b = 0; 
    int i = 0; 
    double ignore = Float.MAX_VALUE; 
    double ignore2 = Float.MAX_VALUE; 

    for (i = 0; i < that.length; i += 2) { 
      a = that[i]; 
      b = that[i+1]; 
      distanceA = calculerDistance(a, b, x, y); 
      if (distanceA < minDistance) { 
       minDistance = distanceA; 
       table[0] = a; 
       table[1] = b; 
      } 
     } 
    ignore = minDistance; 


    for (i = 0; i < that.length; i += 2) { 
      a = that[i]; 
      b = that[i+1]; 
      distanceA = calculerDistance(a, b, x, y); 
      if (distanceA == ignore) { 
       continue; 
       } 
      if (distanceA < minDistance) { 
       minDistance = distanceA; 
       table[2] = a; 
       table[3] = b; 
       } 
      } 
    ignore2 = minDistance; 

    for (i = 0; i < that.length; i += 2) { 
      a = that[i]; 
      b = that[i+1]; 
      distanceA = calculerDistance(a, b, x, y); 
      if ((distanceA == ignore) || (distanceA == ignore2)) { 
       continue; 
       } 
      if (distanceA < minDistance) { 
       minDistance = distanceA; 
       table[2] = a; 
       table[3] = b; 
       } 
      } 

    return table; 
    } 
+2

: 제 3 작업 방법을 작성하고 3 개 가장 근접한 사용자 항목에서 거리와 모든 포인트를 찾을 수 있습니다 배열로 - 그리고 나는 여기에서 2 일 동안 여기에서 찔 렸다. 주어진 숙제는 좋은 조언은 무차별 한 코드를 작성한 다음 나중에 최적화하는 것입니다. 그래서 사용자 입력이 각 다른 모든 점까지의 거리를 찾은 다음 정렬하고 3을 가장 작게 가져옵니다. – mba12

+0

다음 답변에서 내가 한 것을 게시 할 예정입니다. 전체 시간 동안 수행 한 작업과 거의 같습니다. 하지만 제 2와 제 3 거리에 ​​대한 결과는 얻지 못합니다. 하나의 루프에 넣으려고했는데, 아래의 예제처럼 3 개의 루프를 시도했습니다.루프 내에서 루프를 시도했으나 여전히 결과가 없습니다. ( – Frenchie

+0

두 번째 및 세 번째 점을 찾기 전에'minDistance'를'Float.MAX_VALUE'로 재설정해야합니다. 그렇지 않으면 모든 점 – nhouser9

답변

0

나는 프랑스어 그렇게 말을하지 않습니다를 I 코드를 읽기가 어렵습니다. 그러나 다음과 같이 생각하십시오.

사용자 항목에 가장 가까운 지점을 계산하는 방법이 있습니다. 이제 이미 발견 된 지점을 제외하고 사용자 입력 에 가장 가까운 지점을 계산할 수있게 해주는 해당 메소드의 사본을 만들어야합니다.. 그러면 첫 번째와 두 번째로 가까운 점을 찾을 수 있습니다. 그런 다음 이미 발견 한 두 점을 제외하여 세 번째 점을 찾기 위해 똑같은 일을하십시오.

기존 방법의 사본을 만들 수 있습니다. 그것은 다음과 같이 보일 수 있습니다

public static int plusProche (int x, int y, int[] coordonneesHabitations, int ignoreIndex) { 
    double distanceA = 0.0; 
    int k = x; 
    int z = y; 
    int a = 0; 
    int b = 0; 
    int [] that = Arrays.copyOf(coordonneesHabitations, coordonneesHabitations.length); 
    int taille = that.length; 
    int i = 0; 
    double minDistance = Float.MAX_VALUE; 
    int position = 0; 

     for (i = 0; i < taille; i += 2) { 

      //here we add the ability to skip the passed index 
      if ((i/2) == ignoreIndex) { 
       continue; 
      } 

      a = that[i]; 
      b = that[i+1]; 
      distanceA = calculerDistance(a, b, k, z); 
      if (distanceA < minDistance) { 
       minDistance = distanceA; 
       position = i/2; 
       System.out.println(i + " " + minDistance); 
      } 
     } 
     return position; 
} 

당신은 인수로 가까운 지점의 인덱스를 전달하여, 두 번째 가까운 지점을 찾을 위를 사용할 수 있습니다. 이 인덱스를 건너 뛸 것이므로 가장 가까운 다음 인덱스를 찾습니다. 비슷한 점을 세 번째로 가까운 점을 찾으십시오. 이 작업에 대해서는

는 경우 누군가가해야 할 수도 있습니다에, 작동하는 솔루션이있다
+0

그래, 이런 걸 찾고 있었어. 시도해 볼게. 고마워. – Frenchie

+0

@Frenchie 도움을 주시면 기꺼이 도와 드리겠습니다. 알려주세요 – nhouser9

+0

나는 단지 1 개의 질문 만하고, 어떤 값은 ignoreIndex가 가지고 있습니까? 처음부터해야 할 일을 알고 있었고, 두 번째 검색에서 첫 번째 값을 제외하는 방법을 알지 못했습니다 ... 다시 한번 감사드립니다. – Frenchie

0

...

public static int[] troisPlusProches (int x, int y, int[] coordonneesHabitations) 
{ 
    LinkedList<Integer> resultArray = new LinkedList<Integer>(); 
    int[] origArr = Arrays.copyOf(coordonneesHabitations, coordonneesHabitations.length); 
    while (resultArray.size() < 6) { 
     int positionInArray = Decharge.plusProche(x, y, origArr); 
     LinkedList<Integer> newArr = new LinkedList<Integer>(); 
     for (int i = 0; i < origArr.length; i = i + 2) { 
      if (i != positionInArray * 2) { 
       newArr.add(origArr[i]); 
       newArr.add(origArr[i + 1]); 
      } else { 
       resultArray.add(origArr[i]); 
       resultArray.add(origArr[i + 1]); 
      } 
     } 
     origArr = new int[newArr.size()]; 
     for (int k = 0; k < origArr.length; k++) { 
      origArr[k] = newArr.get(k); 
     } 
    } 
    int[] intResultArray = new int[resultArray.size()]; 
    for (int l = 0; l < intResultArray.length; l++) { 
     intResultArray[l] = resultArray.get(l); 
    } 
    return intResultArray;