首页 > 代码库 > [POJ1003]Hangover

[POJ1003]Hangover

[POJ1003]Hangover

试题描述

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We‘re assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.

 

技术分享

输入

The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

输出

For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.

输入示例

1.003.710.045.190.00

输出示例

3 card(s)61 card(s)1 card(s)273 card(s)

数据规模及约定

见“输入

题解

先预处理一下 S[k] = 1/1 + 1/2 + 1/3 + ... + 1/k 的前缀和,因为输入最多是 5.20,看到样例非常良心,所以应该不会比 273 大多少,我就预处理了前 100 个。

每个询问二分答案就好了。(或者直接扫一遍也能过)

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <stack>#include <vector>#include <queue>#include <cstring>#include <string>#include <map>#include <set>using namespace std;const int BufferSize = 1 << 16;char buffer[BufferSize], *Head, *Tail;inline char Getchar() {    if(Head == Tail) {        int l = fread(buffer, 1, BufferSize, stdin);        Tail = (Head = buffer) + l;    }    return *Head++;}int read() {    int x = 0, f = 1; char c = getchar();    while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }    while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); }    return x * f;}#define maxn 1010double S[maxn];int main() {	for(int i = 1; i <= maxn - 10; i++) S[i] = S[i-1] + 1.0 / (double)(i + 1);		while(1) {		double x;		scanf("%lf", &x);		if(x == 0.0) break;		int l = 1, r = 1000;		while(l < r) {			int mid = l + r >> 1;			if(S[mid] < x) l = mid + 1; else r = mid;		}		printf("%d card(s)\n", l);	}		return 0;}

 

[POJ1003]Hangover