POJ 1003 Hangover(二分搜索)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define eps 1e-7
int BS(double c){
	int l=0,r=280,mid;
	double k=0;
	while(l<r-1){
		k=0;
		mid=(l+r)/2;
		for(int i=2;i<=mid+1;i++){
			k+=1.0/i;
		}
		if(k>c-eps) r=mid;
		else l=mid;
	}
	return r;
}


int main(){
	double c;
	while(~scanf("%lf",&c)){
		if(c<eps) break;
		printf("%d card(s)\n",BS(c));
	}
	return 0;
}

二分水题

代码:


你可能感兴趣的:(二分)