* 已知平面上若干个点的坐标。
需要求出在所有的组合中,4 个点间平均距离的最小值(四舍五入,保留 2 位小数)。
比如有 4 个点:a,b,c,d,则平均距离是指:ab, ac, ad, bc, bd, cd 这 6 个距离的平均值。
每个点的坐标表示为:横坐标,纵坐标
坐标的取值范围是:1~1000
所有点的坐标记录在 in.txt 中,请读入该文件,然后计算。
注意:我们测试您的程序的时候,in.txt 可能会很大,比如包含上万条记录。
举例:
如果,in.txt 内的值为:
10,10
79
20,20
80,50
10,20
20,10
则程序应该输出:
11.38
/*

*/
package Question40_49;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
public class Question45ErrorDontUnderstand {
public static double exeForward(Vector<Point>vpoints,Vector<Point>tpoints,int index)
{
// for (Point point : tpoints) {
// System.out.print("["+point.x+","+point.y+"]");
// }
// System.out.println();
if(tpoints.size()==4){
double
t=tpoints.get(0).distance(tpoints.get(1))+tpoints.get(0).distance(tpoints.get(2))+tpoin
ts.get(0).distance(tpoints.get(3))
+tpoints.get(1).distance(tpoints.get(2))+tpoints.get(1).distance(tpoints.get(3))+tp
oints.get(2).distance(tpoints.get(3));
t/=6;
//System.out.println(t);
return t;
}else if (index<vpoints.size()) {
Vector<Point>vector1=new Vector<Point>(tpoints);
Vector<Point>vector2=new Vector<Point>(tpoints);
vector2.add(vpoints.get(index));
double min1=exeForward(vpoints, vector1, index+1);
double min2=exeForward(vpoints, vector2, index+1);
80
return Math.min(min1, min2);
}
return Double.MAX_VALUE;
}
public static void main(String[] args) {
try {
File file=new File("in.txt");
FileInputStream fileInputStream=new FileInputStream(file);
InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
Vector<Point>vpoints=new Vector<Point>();
String ts;
while((ts=bufferedReader.readLine())!=null){
String tss[]=ts.split("\\,");
Point point=new Point(Integer.parseInt(tss[0]), Integer.parseInt(tss[1]));
vpoints.add(point);
}
Vector<Point> tpoints=new Vector<Point>();
System.out.println(String.format("%.2f", exeForward(vpoints, tpoints, 0)));
bufferedReader.close();
} catch (FileNotFoundException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

运行结果:

11.38

java实现坐标的更多相关文章

  1. java基础 绘图技术.坦克大战 之java绘图坐标体系(一)

    坐标体系介绍 下图说明了java坐标体系.坐标原点位于左上角,以像素为单位,像素是计算机屏幕上最小的显示单位.在java的坐标系中,第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素:第二个 ...

  2. Java Gui坐标绝对布局

    JFrame 要setLayout 为null setSize 设置大小 setLocation 设置位置 下面是自己准备做的一个QQ群助手,抓的是qun.qq.com的协议 这是界面设计图 运行效果 ...

  3. java基础 绘图技术.坦克大战 之java绘图坐标体系(二)

    功能:在坐标系上绘制坦克 /* * 功能:坦克游戏的1.0 * 1. 画出坦克 * */ package com.tank; import javax.swing.*; import java.awt ...

  4. WUSTOJ 1325: Distance(Java)坐标计算

    题目链接:1325: Distance Description There is a battle field. It is a square with the side length 100 mil ...

  5. java学习笔记(详细)

    java平台 1.J2SE java开发平台标准版 2.J2EE java开发平台企业版 java程序需要在虚拟机上才可以运行,换言之只要有虚拟机的系统都可以运行java程序.不同系统上要安装对应的虚 ...

  6. Java入门:绘制简单图形

    在上一节,我们学习了如何使用swing和awt工具创建一个空的窗口,本节学习如何绘制简单图形. 基本绘图介绍 Java中绘制基本图形,可以使用Java类库中的Graphics类,此类位于java.aw ...

  7. C#基础教程/适合初学者

    C#基础教程 第一章       C#语言基础 本章介绍C#语言的基础知识,希望具有C语言的读者能够基本掌握C#语言,并以此为基础,能够进一步学习用C#语言编写window应用程序和Web应用程序.当 ...

  8. 最强肉坦:RUST多线程

    Rust最近非常火,作为coder要早学早享受.本篇作为该博客第一篇学习Rust语言的文章,将通过一个在其他语言都比较常见的例子作为线索,引出Rust的一些重要理念或者说特性.这些特性都是令人心驰神往 ...

  9. day01-GUI坦克大战01

    JavaGUI-坦克大战 1.Java绘图坐标体系 坐标体系介绍:下图说明了一个Java坐标体系.坐标原点位于左上角,以像素为单位.在Java坐标体系中,第一个是x坐标,表示当前位置为水平方向,距离坐 ...

随机推荐

  1. JUC之CountDownLatch源码分析

    CountDownLatch是AbstractQueuedSynchronizer中共享锁模式的一个的实现,是一个同步工具类,用来协调多个线程之间的同步.CountDownLatch能够使一个或多个线 ...

  2. linux-rpm强制安装跳过依赖包

    [root@localhost ~]# rpm -ivh tigervnc-1.10.80-4.20200317git8b4be5fd.el7.x86_64.rpm --nodeps --force ...

  3. 深入理解Java虚拟机第三版,总结笔记【随时更新】

    最近一直在看<深入理解Java虚拟机>第三版,无意中发现了第三版是最近才发行的,听说讲解的JDK版本升级,新增了近50%的内容. 这种神书,看懂了,看进去了,真的看的很快,并没有想象中的晦 ...

  4. ql的python学习之路-day14

    前言:本节主要学习时间模块time.datetime python中的几种时间表示:1)时间戳  2)格式化的字符串时间 3)struct_time元组格式的时间 time.datetime模块源码: ...

  5. JS函数和对象

    1.函数 isNaN(数据)/parseInt/parseFloat/Number/prompt... 函数分为系统函数和自定义函数 function: 功能体,函数(方法),可以接受若干个数据,返回 ...

  6. day09作业01用户登录与验证

    import timeLoginTime = time.asctime( time.localtime(time.time()) )print ("time %s" % Login ...

  7. Selenium RemoteWebDriver 利用CDP修改User-Agent

    地球人都知道,如果使用selenium时要修改user-agent可以在启动浏览器时添加配置项,如chromeOptions.addArguments("user-agent=xxx&quo ...

  8. 模板:DOM常用场景【表单提交】——javascript结合HTML DOM(或者JQuery)运用

    一.删除行为前的提示 首先要有一个onclick的DOM(点击)事件,和一个JavaScript弹出框:confirm()确认框 <script> function del(){ var ...

  9. Node.js NPM Tutorial: Create, Publish, Extend & Manage

    A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...

  10. poi--读取不同类型的excel表格

    要想根据不同类型excel表格获取其数据,就要先判断其表格类型 poi-api种方法: getCellType    public int getCellType()        Return th ...