Hangover

Hangover
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 131191   Accepted: 63828

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)


#include<stdio.h>
#include<iostream >
using namespace std;
int main()
{
    double len=0.0,overlen=0.0;
    int c;
    while(scanf("%lf",&len) == 1)
    {
        if(len == 0.0)
            return 0;
        else{
            overlen = 0;
            for(c=1;c;c++)
            {
                overlen += 1.0/(c+1);
                if(overlen>=len)
                {
                    break;
                }

            }
            cout<<c<<" card(s)"<<endl;
        }

    }
    return 0;
}

  以上代码为经过学习最终代码。。。自己的水平太差,打的代码有点乱

/*有问题*/
#include <iostream> #include <stdio.h> using namespace std; int main(void) { double len=0.00,templen=0.00; int c=0; while(scanf("%lf",&len)==1) { templen = 0.0; if(0.01<len<=0.50) c=1; else if(5.20>len>0.50) { for(c=0;len>templen;c++) templen+=1.0/(c+2); } else c=0; if(c != 0) cout<<c<<" card(s)"<<endl; else return 0; } return 0; }

出现问题:

warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

很不明白为什么为什么len=0.00会进入这层判断。。。经过研究

在比较float和double类型的时候,
因为float/double精度的问题

比如 1.000000001 可能和1.0000000000001相等

不应该直接使用 a > b 等类似的方式进行比较

而是采用 两个数做差取绝对值然后跟 你指定的精度进行比较
便可得出 两个double/float的大小

第一次的代码逻辑也有问题ying~
不改了……
posted @ 2018-01-03 18:33  卉卉卉大爷  阅读(490)  评论(0编辑  收藏  举报