POJ1003 – 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
Output
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 (基础)的更多相关文章
- [POJ1003]Hangover
[POJ1003]Hangover 试题描述 How far can you make a stack of cards overhang a table? If you have one card, ...
- POJ-1003&1004
这两题比较简单,就不做分析了,描述下题目,就上代码吧. [题目描述] 1003,其实就是求这个方程的最小n:1/2 + 1/3 + 1/4 + ... + 1/(n + 1) >= c: 100 ...
- Hangover[POJ1003]
Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 121079 Accepted: 59223 Descr ...
- 【POJ1003】Hangover(二分搜索)
直接用库函数二分即可. #include <iostream> #include <cstring> #include <cstdlib> #include < ...
- 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题
这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...
- (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!
ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...
- java基础集合经典训练题
第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...
- node-webkit 环境搭建与基础demo
首先去github上面下载(地址),具体更具自己的系统,我的是windows,这里只给出windows的做法 下载windows x64版本 下载之后解压,得到以下东西 为了方便,我们直接在这个目录中 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
随机推荐
- javascript中NAN undefined 和null
null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性. NaN 属性是代表非数字值的特殊值.该属性用于指示某个值不是数字.可以把 Num ...
- C++中 string 的用法大全
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...
- C#设置随机整数
JQuery var x = 5;//最大值var y = 1;//最小值var rand = parseInt(Math.random() * (x - y + 1) + y); Mvc控制器 Ra ...
- Linux下如何查看tomcat是否安装、启动、文件路径、进程ID
Linux下如何查看tomcat是否安装.启动.文件路径.进程ID 在Linux系统下,Tomcat使用命令的操作! 检测是否有安装了Tomcat: rpm -qa|grep tomcat 查看Tom ...
- Jsの练习-数组常用方法 -splice()
splice() 功能:1.可以实现删除,插入和替换. 删除:可以删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数. 例如:splice(0,2)会删除数组中的前2项. 插入:可 ...
- RHEL 6 和 RHEL 7 的一些有关运行级别,服务管理,服务启动等方面的区别介绍
systemd是7中的新命令组,集成了service和chkconfig的功能.system命令可参考:https://www.cnblogs.com/ray-bk/p/10415173.html 运 ...
- IC卡T0协议中的过程字与状态字
T0协议中,IC卡收到命里头以后向TTL回传过程字或状态字SW1.SW2: TTL和IC卡在二者之间的命令和数据交换的任何时刻都必须知道数据流的方向和IO线路由谁驱动. 摘录参考:<中国金融集成 ...
- Git源码安装 Linux指定安装目录
1.安装依赖包 $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel 2.下载最新版源码包https: ...
- requests.get() 的 headers 参数
官方文档requests.get()方法的定义如下: 源码如下: 看到最后一行return,get方法最后是通过调用requests.request 方法实现的,其实在其它的请求方法如post,put ...
- 处理csv和json数据
CSV是以逗号为分隔符,存储数据的文件. 流程:1.filename存储文件名.2.打开文件并存储在文件对象f中.3.将f传递给csv.reader()创建阅读器对象.4.next()获取第一行. e ...