zoukankan      html  css  js  c++  java
  • ZOJ Problem Set – 1045 HangOver

         真的是不行了,很长时间没有玩过这玩意儿了,一下子上来,还真是很不习惯。碰了很多壁,终于碰到了这么一道相对比较简单的题目,也算是捡回一点点自信吧,希望以后会越走越好。

          下面先给出原题:

    ZOJ Problem Set - 1045

    HangOver


    Time Limit: 1 Second      Memory Limit: 32768 KB


    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.

    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.
    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.

    Example input:
    1.00
    3.71
    0.04
    5.19
    0.00

    Example output:
    3 card(s)
    61 card(s)
    1 card(s)
    273 card(s)


    Source: Mid-Central USA 2001

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=45

    题目不难,直接给出代码吧:

      1: #include<iostream>
    
      2: #include<vector>
    
      3: 
    
      4: using namespace std;
    
      5: 
    
      6: const double MINVALUE = 0.00000001;
    
      7: void IntialPro(vector<double>& vec)
    
      8: {
    
      9:     double hangLength = 0.0;
    
     10:     for(int n = 1;hangLength <= 5.20; n++)
    
     11:     {
    
     12:         hangLength += 1/static_cast<double>(n + 1);
    
     13:         vec.push_back(hangLength);
    
     14:     }
    
     15: }
    
     16: int main(void)
    
     17: {
    
     18:     vector<double> hangOver;
    
     19:     IntialPro(hangOver);
    
     20: 
    
     21:     double input;
    
     22: 
    
     23:     while(cin>>input)
    
     24:     {
    
     25:         if(input > -1*MINVALUE && input < MINVALUE)
    
     26:         {
    
     27:             break;
    
     28:         }
    
     29:         for(size_t i = 0; i < hangOver.size(); i++)
    
     30:         {
    
     31:             if(hangOver[i] > input)
    
     32:             {
    
     33:                 cout<<i + 1<<" card(s)"<<endl;
    
     34:                 break;
    
     35:             }
    
     36:         }
    
     37:     }
    
     38:     return 0;
    
     39: }

    在这段代码中,没有什么特别的需要主要的,唯一让我感觉到有点意思的是double类型与0.00进行比较的时候的特点。众所周知,double类型在内部存储的时候是存在精确缺失的,所以,要让double与0进行比较的时候,不能直接来,需要定义一个很小的值,就像我程序中的那样进行比较。这种技巧,我也是在某次朋友说要考考我的时候得知的。也许在验证条件不是很苛刻的时候,double直接与0比较的时候也能通过,但是一旦比较严格的话,无疑会失败的。

           这同时,让我想到了其它的几个比较。也是编程习惯的问题(在《高质量C、C++编程指南》中有讲)。如下:
    bool值的比较(其中flag是一个bool类型的变量)

       if(flag)

    指针类型与零的比较(其中p的定义为:char *p;):

       if(p == NULL)
  • 相关阅读:
    Windows共享作为公司文件服务器的案例
    关于限制域用户登陆计算机的几点总结
    防止加入域主机脱离域的控制(管理员权限)
    域组策略禁止执行指定软件
    域用户执行金蝶K/3报错解决方法
    WinXP/Win7/Win8本地用户配置文件迁移至域用户
    Windows批处理:请求远程协助
    浅谈JavaScript闭包
    新蛋那几月
    JavaScript 32位整型无符号操作
  • 原文地址:https://www.cnblogs.com/malloc/p/1671453.html
Copyright © 2011-2022 走看看