Links: http://poj.org/problem?id=1003
Hangover
Time limit:1000ms Memory limit:10000ktotal submissions:109695 accepted:53525Description
How far can I make a stack of cards overhang a table? If you had one card, you can create a maximum overhang of half a card length. (We ' re assuming that the cards must is perpendicular to the table.) With both cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the TA Ble by a third of a card length, for a total maximum overhang of a + 1/3 = 5/6 card lengths. In general you can make n cards overhang by + 1/3 + + + + + ... + 1/(n + 1) card lengths, where the top card overhangs t He second by, the second overhangs tha third by 1/3, the third overhangs the fourth by, etc., and the bottom card Overhangs the table by 1/(n + 1). This was illustrated in the below.
InputThe input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the Put. Each test case was a single line containing a positive floating-point number C whose value was at least 0.01 and at the most 5.2 0; C'll contain exactly three digits.
Outputfor each test case, the output the minimum number of cards necessary to achieve a overhang of at least C card lengths. Use the exact output format shown in the examples.
Sample Input1.00
3.71
0.04
5.19
0.00
Sample Output3 Card (s)
A-card (s)
1 Card (s)
273 Card (s)
Source
Mid-Central USA 2001
The main idea--card a cover one, the 1th can extend up to 1/2 of the length, the second to extend the length of up to 1/3, and so on. Q: To give you a length, you need to calculate at least a few cards to reach.
Ideas-The maximum length is only 5.2, so the maximum is 278, so constantly enumerate.
Complexity analysis--time complexity: O (n), spatial complexity: O (1)
Attach the AC code:
#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 Doub Le PI = 3.14159265;const double E = 2.71828182846;int Main () {using namespace Std;std::ios::sync_with_stdio (false);d ouble Len;while (Cin >> len && len > 0) {Double sum = 0;int i;for (i=2; sum<len; i++) sum + = 1.0/i;cout <&L T I-2 << "card (s) \ n";} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1003 Hangover