九度OJ_Freckles_1144

<span style="font-family: 'Microsoft Yahei'; background-color: rgb(255, 255, 255);">题目1144:Freckles</span>

题目描述:

    In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. 
    Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle. 

输入:

    The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出:

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入:
3
1.0 1.0
2.0 2.0
2.0 4.0
样例输出:
3.41
AC代码:

<span style="font-family:Microsoft YaHei;font-size:14px;">import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		DecimalFormat df = new DecimalFormat("######0.00");
		Comparator<road> sortByWeight = new Comparator<Main.road>() {
			@Override
			public int compare(road r1, road r2) {
				if(r1.weight>=r2.weight){
					return 1;
				}else{
					return -1;
				}
			}
		};
		while(cin.hasNext()){
			int num = cin.nextInt();
			ArrayList<point> points = new ArrayList<point>();
			ArrayList<road> roads = new ArrayList<road>();
			for(int i = 0;i<num;i++){
				point point = new point();
				point.x = cin.nextDouble();
				point.y = cin.nextDouble();
				point.index = i+1;
				points.add(point);
			}
			buildRoad(points,roads);
			Collections.sort(roads,sortByWeight);
			int tree[] = new int[roads.size()+1];
			for(int i = 0;i<=roads.size();i++){
				tree[i] = -1;
			}
			double result = 0;
			for(int i = 0;i<roads.size();i++){
				int a = findRoot(roads.get(i).a,tree);
				int b = findRoot(roads.get(i).b, tree);
				if(a!=b){
					tree[a] = b;
					result+=roads.get(i).weight;
				}
			}
			System.out.println(df.format(result));
		}
	}
	private static int findRoot(int x, int[] tree) {
		if(tree[x] == -1){
			return x;
		}else{
			int temp = findRoot(tree[x], tree);
			tree[x] = temp;
			return temp;
		}
	}
	private static void buildRoad(ArrayList<point> points, ArrayList<road> roads) {
		for(int i = 0;i<points.size();i++){
			for(int n = i+1;n<points.size();n++){
				road road = new road();
				road.a = points.get(i).index;
				road.b = points.get(n).index;
				road.weight = Math.sqrt(Math.abs(</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">                                 (points.get(i).x-points.get(n).x)
				*(points.get(i).x-points.get(n).x)
			+(points.get(i).y-points.get(n).y)*(points.get(i).y-points.get(n).y)));
				roads.add(road);
			}
		}
	}
	public static class point{
		public int index;
		public double x;
		public double y;
	}
	public static class road{
		public int a;
		public int b;
		public double weight;
	}
}</span><span style="font-family:Microsoft Yahei;font-size: 18px;">
</span>

总结:

1.java保留两位小数方法:

  DecimalFormat df = new DecimalFormat("######0.00");
  System.out.println(df.format(result));
2.java幂函数Math.pow(a,2)表示a的平方

3.java开方函数Math.sqrt(double x),绝度值函数Math.abs()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值