Hangover(poj 1003)

来源:互联网 发布:js的面向对象编程理解 编辑:程序博客网 时间:2024/06/10 11:56

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.00
3.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之间),输出至少需要多少张卡片。

2 算法思路

非常简单的题目,第i次的长度为1i+1。还需要考虑的就是0的情况,因为浮点数无法直接判断相等,但是因为精度就小数点后2位,可以先乘以100在判断是否为0

3 算法实现

#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 数据测试

这里写图片描述

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肉丛蓉功效与作用 肉丛蓉功效 丛林深处 丛林拼音 丛林总动员 丛林之狐 丛林的拼音 丛林奇航 丛林狼作品 丛林女超人 激情丛林 丛林地狱 丛林奇兵 丛林噩梦 丛林之王 丛林欢乐世界 丛林之王号大虫是什么生肖 丛林有情狼 丛林王 丛林野食王 丛林野食王第二季 丛林之王是什么动物 泰山归来之丛林之王 丛林虎啸 丛林豹美发剪刀 丛林豹 苍竹 丛明晨 丛组词 丛的组词 丛字组词 丛怎么组词 丛的组词有那些 丛字组词有哪些 丛连彪 东侨开发区 东石笋风景区 东海岛旅游度假区 准东经济技术开发区 东十区 济宁人民医院东院区