Hangover
 

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)

Source

 
  根据题意可建立以下数学模型:
     令 ∑(1/n) >= c
     其中 n∈[2, ∞), c∈[0.01, 5.20]且其精度含小数在内最多3个数字
     给定c 求 n (若c在范围外,则不求解)
 
    分析:
     本质就是变种的调和数列求和(数列中缺少1)
     但调和数列是发散的,不存在准确的求和公式,只有近似公式:
      调和数列 ∑(1/n) ~ ln(n+1) + R
      其中 n∈[1, ∞), R为欧拉常数(R = 0.5772156649...)
 
     但近似公式只有在n非常大的时候误差才可以忽略不计,
     当n很小时,对本题而言误差是不可接受的。
 
     因此本题用常规解法即可
     (由于前n项和是固定的,用打表法也可, 不过题目考核范围较小,打表意义也不大)
#include <iostream>
using namespace std; /*
* 根据调和数列的和值反求项数
* @param sum 目标和值
* return 调和数列项数
*/
int harmonicSeries(double sum); int main(void) {
double sum = 0.0;
while(true) {
cin >> sum;
if(sum < 0.01 || sum > 5.20) {
break;
} int n = harmonicSeries(sum);
cout << n << " card(s)" << endl;
}
return ;
} int harmonicSeries(double sum) {
int n = ;
double curSum = 0.0;
while(curSum < sum) {
curSum += (1.0 / n++);
}
return n - ; // n从2开始因此项数-1, 最后一次求和多了一次n++也要-1, 因此共-2
}

POJ1003 – Hangover (基础)的更多相关文章

  1. [POJ1003]Hangover

    [POJ1003]Hangover 试题描述 How far can you make a stack of cards overhang a table? If you have one card, ...

  2. POJ-1003&1004

    这两题比较简单,就不做分析了,描述下题目,就上代码吧. [题目描述] 1003,其实就是求这个方程的最小n:1/2 + 1/3 + 1/4 + ... + 1/(n + 1) >= c: 100 ...

  3. Hangover[POJ1003]

    Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121079   Accepted: 59223 Descr ...

  4. 【POJ1003】Hangover(二分搜索)

    直接用库函数二分即可. #include <iostream> #include <cstring> #include <cstdlib> #include < ...

  5. 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

    这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...

  6. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

  7. java基础集合经典训练题

    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...

  8. node-webkit 环境搭建与基础demo

    首先去github上面下载(地址),具体更具自己的系统,我的是windows,这里只给出windows的做法 下载windows x64版本 下载之后解压,得到以下东西 为了方便,我们直接在这个目录中 ...

  9. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

随机推荐

  1. 基于 HTML5 的工业组态高炉炼铁 3D 大屏可视化

    前言 在大数据盛行的现在,大屏数据可视化也已经成为了一个热门的话题.大屏可视化可以运用在众多领域中,比如工业互联网.医疗.交通.工业控制等等.将各项重要指标数据以图表.各种图形等形式表现在一个页面上, ...

  2. 用python写一个定时提醒程序

    身体是革命的本钱,身体健康了我们才有更多精力做自己想做的事情,追求女神,追求梦想.然而程序员是一个苦比的职业,大部分时间都对着电脑,我现在颈椎就不好了,有时候眼睛还疼,我还没20阿,伤心...于是乎写 ...

  3. LAMP架构(三)

    第十九课 LAMP架构(三) 目录 一.配置防盗链 二.访问控制Directory 三.访问控制FilesMatch 四.限定某个目录禁止解析php 五.限制user_agent 六.php相关配置 ...

  4. copy GC 和 mark & compaction GC的算法异同

    先标记 然后 copy GC是,对所有child,判断, 如果child没有被访问过,那么拷贝到新地址,child的forwording指向新地址,child标记为已访问,把自己对child的引用改为 ...

  5. Hadoop学习笔记05_HA

    ################# HA 即 High Available 高可用.# 其作用是为了减少主从结构的单点故障,而设置备用节点,既然学习了Hadoop生态圈,那么HA配置也是必须要掌握的. ...

  6. SVN 的搭建及使用(一)下载和搭建SVN服务器

    (本文是从博客园上的文章改编而来,其中有些关于版本问题的截图是直接引用原文的,与当前版本有可能不同) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和 ...

  7. FCC JS基础算法题(12):Where do I belong(数组排序并找出元素索引)

    题目描述: 先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引.举例:where([1,2,3,4], 1.5) 应该返回 1.因为1.5插入到数组[1,2,3,4]后变成[1,1.5 ...

  8. sed语法

    Sed 命令行 以下是我们可以指定单引号在命令行sed命令的格式如下: sed [-n] [-e] 'command(s)' files 例子 考虑一下我们有一个文本文件books.txt待处理,它有 ...

  9. linux下vim的安装及其设置细节

    第一步:使用apt安装vim sudo apt-get install vim 第二步:行号及其tab建设置 vim ~/.vimrc 添加如下文字 set nu //代码显示行号syntax on ...

  10. 12--Python入门--文件读写--TXT文件

    在进行数据分析之前,可能需要读写自己的数据文件.或者在完成数据分析之后,想把结果输出到外部的文件在Python中,利用pandas模块中的几个函数,可以轻松实现这些功能,利用pandas读取文件之后数 ...