首页 > 专题系列 > Java解POJ > POJ 2365 Rope [解题报告] Java
2013
11-11

POJ 2365 Rope [解题报告] Java

Rope

问题描述 :

Plotters have barberically hammered N nails into an innocent plane shape, so that one can see now only heads. Moreover, pursuing their mean object, they have hammered all the nails into the vertices of a convex polygon. After that they…it is awful… have roped off the nails, so that the shape felt upset (the rope was very thin). They’ve done it as it is shown in the figure.



Your task is to find out a length of the rope.

输入:

There two numbers in the first line of the standard input: N — a number of nails (1 <= N <= 100), and a real number R — a radius of heads of nails. All the heads have the same radius. Further there are N lines, each of them contains a pair of real coordinates (separated by a space) of centers of nails. An absolute value of the coordinates doesn't exceed 100. The nails are described in a clockwise order starting from an arbitrary nail. Heads of different nails don't adjoin.

输出:

The standard output should contain in its only line a real number with two digits precision (after a decimal point) — a length of the rope.

样例输入:

4 1
0.0 0.0
2.0 0.0
2.0 2.0
0.0 2.0

样例输出:

14.28

解题代码:

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
 public static void main(String[] args){
  Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
	int n=scanner.nextInt();
	double r=scanner.nextDouble();
	double[] x=new double[n];
	double[] y=new double[n];
	for (int i=0;i< n ;i++ ){
		x[i]=scanner.nextDouble();
		y[i]=scanner.nextDouble();
	}
	double l=2*r*Math.PI;
	for (int i=0;i < n ;i++ ){
          if (i!=n-1){
	    l=l+Math.sqrt((x[i]-x[i+1])*(x[i]-x[i+1])+(y[i]-y[i+1])*(y[i]-y[i+1]));
	  }
	 else{
	   l=l+Math.sqrt((x[i]-x[0])*(x[i]-x[0])+(y[i]-y[0])*(y[i]-y[0]));
		}
	}
	l=Math.round(l*100.0)/100.0;
	System.out.println(l);
   }
}

  1. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了

  2. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.