首页 > 专题系列 > Java解POJ > POJ 2507 Crossed ladders [解题报告] Java
2013
11-11

POJ 2507 Crossed ladders [解题报告] Java

Crossed ladders

问题描述 :

A narrow street is lined with tall buildings. An x foot long ladder is rested at the base of the building on the right side of the street and leans on the building on the left side. A y foot long ladder is rested at the base of the building on the left side of the street and leans on the building on the right side. The point where the two ladders cross is exactly c feet from the ground. How wide is the street?

输入:

Each line of input contains three positive floating point numbers giving the values of x, y, and c.

输出:

For each line of input, output one line with a floating point number giving the width of the street in feet, with three decimal digits in the fraction.

样例输入:

30 40 10
12.619429 8.163332 3
10 10 3
10 10 1

样例输出:

26.033
7.000
8.000
9.798

解题代码:

//* @author: [email protected]
import java.io.*;
import java.util.Arrays;
public class Main
{	
 public static void main(String[] args) throws IOException
  {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  while(in.ready())
   {
	String[] ss=in.readLine().split(" ");
	double x=Double.parseDouble(ss[0]);
	double y=Double.parseDouble(ss[1]);
	double c=Double.parseDouble(ss[2]);
	double max=Math.min(x, y),min=0;
	for(int i=0;max-min>0.00001;i++)
	{
         double mid=(min+max)/2;
	 double d1=Math.sqrt(x*x-mid*mid);
	 double d2=Math.sqrt(y*y-mid*mid);
	 if(c*(d1+d2)==(d1*d2)){
		min=max=mid;
	        break;
	 }
	else if(c*(d1+d2)>(d1*d2)) max=mid;
	else min=mid;
       }
       System.out.printf("%.3f\n",(min+max)/2);
    }
  }
}

  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  3. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。

  4. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish