zoukankan      html  css  js  c++  java
  • 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~
    不改了……
  • 相关阅读:
    Socket 之 同步以及异步通信
    Socket 之 c#实现Socket网络编程
    Socket 之 API函数介绍
    Socket 之 原理与编程基础
    C# 之 user32函数库
    WinServer 之 访问同网段服务器 或 同一服务器多虚拟机间的访问
    annex-b格式
    FLV文件格式解析
    PHP5中的stdClass
    web服务器【apache/nginx] 关闭目录的浏览权限
  • 原文地址:https://www.cnblogs.com/yxh-amysear/p/8185120.html
Copyright © 2011-2022 走看看