Hangover(poj 1003)
2017-12-25 22:37
423 查看
1 问题描述
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)
大致意思
若将一叠卡片放在一张桌子的边缘,你能放多远?如果你有一张卡片,你最远能达到卡片长度的一半。(我们假定卡片都正放在桌 子上。)如果你有两张卡片,你能使最上的一张卡片覆盖下面那张的1/2,底下的那张可以伸出桌面1/3的长度,即最远能达到 1/2 + 1/3 = 5/6 的卡片长度。一般地,如果你有n张卡片,你可以伸出 1/2 + 1/3 + 1/4 + … + 1/(n + 1) 的卡片长度,也就是最上的一张卡片覆盖第二张1/2,第二张超出第三张1/3,第三张超出第四张1/4,依此类推,最底的一张卡片超出桌面1/(n + 1)。现在给定伸出长度C(0.00至5.20之间),输出至少需要多少张卡片。题目地址poj 1003
2 算法思路
非常简单的题目,第i次的长度为1i+1。还需要考虑的就是0的情况,因为浮点数无法直接判断相等,但是因为精度就小数点后2位,可以先乘以100在判断是否为03 算法实现
#include <iostream> #include <cstdio> using namespace std; int main() { double val; while(cin >> val) { // double无法直接判断相等,可以乘100转成int判断是否是0 if((int)(val*100) == 0) return 0; int i; double len = 0.00; for(i = 1; true; ++ i) { len += 1.0/(i+1); // 大于输入的值就结束 if(len > val) { printf("%d card(s)\n", i); break; } } } return 0; }
4 数据测试
相关文章推荐
- POJ1003-Hangover
- poj 1003 Hangover
- poj 1003 Hangover
- POJ 1003 Hangover
- POJ 1003 Hangover(水题) 权当复习…
- POJ 1003--Hangover
- POJ 1003 Hangover
- poj 1003 (nyoj 156) Hangover
- poj 1003 Hangover 解题报告
- POJ-1003 Hangover-纸牌叠放
- POJ 1003:Hangover
- poj 1003:Hangover(水题,数学模拟)
- POJ - 1003 Hangover
- poj 1003 Hangover
- poj_1003——Hangover
- POJ 1003 Hangover
- poj 1003 && hdu 1056 HangOver
- POJ-1003-Hangover
- POJ 1003 hangover
- ACM学习-POJ-1003-Hangover