ZOJ 3203 Light Bulb

題目:http://openoj.awaysoft.com:8080/judge/problem/viewProblem.action?id=14179

三分入門題目

三分:

區間上求凸(凹)點時,把區間ad分爲 abcd

其中 b = ( a+d) / 2 ;

c = ( b + d) / 2;

比較b和c哪個更好,依次類推。


#include <cstdio>
#include <iostream>
using namespace std;
double H,h,d,x,l,r,ans;
double hid(double x){					// x 表示人到牆的距離
	return (h*d-H*x)/(d-x)+x;
}
int main(){
	int N;
	cin >> N;
	while (N--){
		cin >> H >> h >> d;
		ans = 0;
		for (l=0,r=(d*h)/H;l+0.0001<r;){
			double mid = (l+r)/2;
			double mid2 = (mid+r)/2;
			if (hid(mid) >= hid(mid2)){
				r = mid2;
			}else {
				l = mid;
			}
		}
		printf("%.3lf\n",hid(l));
	}
	return 0;
}

 
發佈了59 篇原創文章 · 獲贊 14 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章