7-7 编程练习题答案

import java.util.Scanner;

public class Test
{
	public static void main(String[] args)
	{
		double[][] points = {
			{-1, 0, 3}, {-1, -1, -1}, {4, 1, 1}, 
			{2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3}, 
			{-1.5, 4, 2}, {5.5, 4, -0.5}
			};

		double minestDistance = 999;
		int index1 = 0;
		int index2 = 0;

		for(int i = 0; i < points.length; i++)
		{
			for(int j = i + 1; j < points[i].length; j++)
			{
				if(minestDistance > distance(points[i][0], points[j][0], points[i][1], points[j][1], points[i][2], points[j][2]))
				{
					minestDistance = distance(points[i][0], points[j][0], points[i][1], points[j][1], points[i][2], points[j][2]);
					index1 = i;
					index2 = j;
				}	
			}
		}
		
		System.out.println("The closest two points are (" + points[index1][0] + ", " + points[index1][1] + ", " + points[index1][2] + ") and (" + points[index2][0] + ", " + points[index2][1] + ", " + points[index2][2] + ")");
	}

	public static double distance(double x1, double y1, double z1, double x2, double y2, double z2)
	{
		return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值