HW7.7

 1 public class Solution
 2 {
 3     public static void main(String[] args)
 4     {
 5         double[][] points =
 6         {
 7             {-1, 0, 3}, {-1, -1, -1},
 8             {4, 1, 1}, {2, 0.5, 9},
 9             {3.5, 2, -1}, {3, 1.5, 3},
10             {-1.5, 4, 2}, {5.5, 4, -0.5}
11         };
12
13         double shortestDistance = distance(points[0][0], points[0][1], points[0][2],
14             points[1][0], points[1][1], points[1][2]);
15         double currentDistance = shortestDistance;
16
17         for(int i = 0; i < points.length; i++)
18         {
19             for(int j = i + 1; j < points.length; j++)
20             {
21                 currentDistance = distance(points[i][0], points[i][1], points[i][2],
22                     points[j][0], points[j][1], points[j][2]);
23                 if(currentDistance < shortestDistance)
24                     shortestDistance = currentDistance;
25             }
26         }
27
28         System.out.println("The shortest distance is " + shortestDistance);
29     }
30
31     public static double distance(double x1, double y1, double z1, double x2, double y2, double z2)
32     {
33         double square = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1);
34         return Math.sqrt(square);
35     }
36 }
时间: 2024-10-07 04:17:38

HW7.7的相关文章

HW7.12

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35}; 8 int[][] brackets = 9 { 10 {8350, 33950, 82250, 171550, 372950}, 11 {16700, 67900, 137050

HW7.14

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter the size for the matrix: "); 9 int size = input.nextInt(); 10 11 input.clo

HW7.13

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter the number of rows and columns of the array: "); 9 int row = input.nextInt

HW7.3

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 char[][] answers = 6 { 7 {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, 8 {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, 9 {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', '

HW7.18

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 int[][] m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}; 6 7 shuffle(m); 8 9 for(int i = 0; i < m.length; i++) 10 { 11 for(int j = 0; j < m[0].length; j++) 12 System.out.print(

HW7.17

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 int n = input.nextInt(); 10 int limit = input.nextInt(); 11 12 int[][] borrowers = new int[n][n]; 13

HW7.1

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 double[][] matrix = new double[4][4]; 9 System.out.print("Enter a 4-by-4 matrix row by row: "); 10

HW7.2

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 int[][] matrix = new int[4][4]; 9 System.out.println("Enter a 4-by-4 matrix row by row: "); 10 for

HW7.16

1 import java.util.Arrays; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 int row = (int)(Math.random() * 10); 8 int column = (int)(Math.random() * 10); 9 int[][] array = new int[row][column]; 10 11 for(int i = 0; i < ro

HW7.11

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter a number between 0 and 511: "); 9 int number = input.nextInt(); 10 11 inpu