Description
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.
Input
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.Output
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.Sample Input
1.003.71
0.04
5.19
0.00
Sample Output
3 card(s)61 card(s)
1 card(s)
273 card(s)
Source
Mid-Central USA 2001
大意——卡片一张盖一张,第1张最多可以延伸1/2的长度,第二张最多延伸1/3的长度,依此类推。问:给你一个长度,要你计算最少需要几张卡片才能达到。
思路——最大长度只有5.2,所以最多也就278张,因此不断枚举即可。
复杂度分析——时间复杂度:O(n),空间复杂度:O(1)
附上AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double PI = 3.14159265;
const double E = 2.71828182846;
int main()
{
using namespace std;
std::ios::sync_with_stdio(false);
double len;
while (cin >> len && len > 0)
{
double sum = 0;
int i;
for (i=2; sum<len; i++)
sum += 1.0/i;
cout << i-2 << " card(s)\n";
}
return 0;
}