双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

程序输出结果不对啊

atinysix atinysix 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

import java.util.Scanner;
public class FindNearestPoints{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the numbers of points: ");
int numberOfPoints = input.nextInt();

double[][] points = new double[numberOfPoints][2];
System.out.print("Enter " + numberOfPoints + " points: ");
for(int i = 0; i < points.length; i++){
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
}

int p1 = 0, p2 = 0;
double shortestDistance = distance(points[p1][0], points[p1][1], points[p2][0], points[p2][1]);

for(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;
}
}
}

System.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));
}
}

dai@ubuntu:~/Programs/java$ javac FindNearestPoints.java
dai@ubuntu:~/Programs/java$ java FindNearestPoints
Enter the numbers of points: 3
Enter 3 points: 1 1 4 -3 4 9
The closest two points are (1.0,1.0) and (1.0,1.0)
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
atinysix
Java SE 62,621 成员 307,251 内容