Java黑皮书课后题第8章:*8.8(所有最近的点对)修改程序清单8-3,找出所有所有具有最小距离的点对。下面是一个运行示例
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第8章:*8.8(所有最近的点对)修改程序清单8-3,找出所有所有具有最小距离的点对。下面是一个运行示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*8.8(所有最近的點對)修改程序清單8-3,找出所有所有具有最小距離的點對。下面是一個運行示例
- 題目
- 題目描述與運行示例
- 程序清單8-3
- 破題
- 代碼
題目
題目描述與運行示例
*8.8(所有最近的點對)修改程序清單8-3,找出所有所有具有最小距離的點對。下面是一個運行示例:
Enter the number of points: 8 Enter 8 points: 0 0 1 1 -1 -1 2 2 -2 -2 -3 -3 -4 -4 5 5 The closest two points are (0.0, 0.0) and (1.0, 1.0) The closest two points are (0.0, 0.0) and (-1.0, -1.0) The closest two points are (1.0, 1.0) and (2.0, 2.0) The closest two points are (-1.0, -1.0) and (-2.0, -2.0) The closest two points are (-2.0, -2.0) and (-3.0, -3.0) The closest two points are (-3.0, -3.0) and (-4.0, -4.0) Their distance is 1.4142135623730951程序清單8-3
import java.util.Scanner;public class qingdan {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the number of points: ");int numberOfPoints = input.nextInt();// Create an array to store pointsdouble[][] points = new double[numberOfPoints][2];System.out.println("Enter " + numberOfPoints + " points: ");for (int i = 0 ; i < points.length ; i++){points[i][0] = input.nextDouble();points[i][1] = input.nextDouble();}// p1 and p2 are the indices in the points' arrayint p1 = 0, p2 = 1; //Initial two pointsdouble shortestDistance = distance(points[p1][0], points[p1][1],points[p2][0], points[p2][1]);// Compute distance for every two pointsfor (int i = 0 ; i < points.length ; i++){for (int j = i+1 ; j < points.length ; j++){double distance = distance(points[i][0], points[i][1],points[j][0], points[j][1]);if (shortestDistance > distance){p1 = i;p2 = j;shortestDistance = distance;}}}// Display resultSystem.out.println("The closest two points are " +"(" + points[p1][0] + ", " + points[p1][1] + ") and (" +points[p2][0] + ", " + points[p2][1] + ")");}public static double distance(double x1, double y1, double x2, double y2){return Math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));} }破題
代碼
import java.util.Scanner;public class Test8_8 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the number of points: ");int numberOfPoints = input.nextInt();// Create an array to store pointsdouble[][] points = new double[numberOfPoints][2];System.out.println("Enter " + numberOfPoints + " points: ");for (int i = 0 ; i < points.length ; i++){points[i][0] = input.nextDouble();points[i][1] = input.nextDouble();}// p1 and p2 are the indices in the points' arrayint p1 = 0, p2 = 1; //Initial two pointsdouble shortestDistance = distance(points[p1][0], points[p1][1],points[p2][0], points[p2][1]);// Compute distance for every two pointsfor (int i = 0 ; i < points.length ; i++){for (int j = i+1 ; j < points.length ; j++){double distance = distance(points[i][0], points[i][1],points[j][0], points[j][1]);if (shortestDistance > distance){p1 = i;p2 = j;shortestDistance = distance;}}}// Display resultfor (int i = 0 ; i < points.length ; i++){for (int j = i+1 ; j < points.length ; j++){double distance = distance(points[i][0], points[i][1],points[j][0], points[j][1]);if (shortestDistance == distance){System.out.println("The closest two points are " +"(" + points[i][0] + ", " + points[i][1] + ") and (" +points[j][0] + ", " + points[j][1] + ")");}}}//輸出長度System.out.println("Their distance is " + shortestDistance);}public static double distance(double x1, double y1, double x2, double y2){return Math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第8章:*8.8(所有最近的点对)修改程序清单8-3,找出所有所有具有最小距离的点对。下面是一个运行示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第8章:*8.7(距
- 下一篇: Java黑皮书课后题第8章:8.9(井字