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的开发者 ...
随机推荐
- .net core部署到Ubuntu碰到的问题
数据库连接的时候,会报错“MySql.Data.MySqlClient.MySqlException:“The host localhost does not support SSL connecti ...
- 1分钟看懂log4j 配置自己想要的日志信息
在开发的时候我们会希望 只将 sql信息的日志,已经自定义输出的日志进行打印 ,而一些框架级的日志不需要输出 如下 首先 rootLogger 设置日志级别 log4j.rootLogg ...
- ERROR in Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (64)
该问题说的是当前环境不支持node-sass,网上说了一下是要安装node 7一下版本才支持. 这里改使用less-loader,及less
- Nearest Common Ancestors (POJ 1330)
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- SQL-43 将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。
题目描述 将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01.CREATE TABLE IF NOT EXISTS titles_test ...
- TensorFlow Training 优化函数
tf.train 提供了一组帮助训练模型的类和函数. 优化器 优化器基类提供了计算渐变的方法,并将渐变应用于变量.子类的集合实现了经典的优化算法,如 GradientDescent和Adagrad. ...
- jsp弹窗并跳转
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- [转]keepalived简介
https://www.jianshu.com/p/b050d8861fc1 contents: 什么是Keepalived VRRP协议简介 Keepalived原理 Keepalived配置文件详 ...
- Thrift-0.10.0 CenOS 7 编译错误 error: expected ')' before 'PRIu32'
Thrift-0.10.0 CenOS 7 编译错误 error: expected ')' before 'PRIu32' 在编译Thrift的时候,无论是Apache官网tar包,还是Github ...
- Map集合、HashMap集合、LinkedHashMap集合、Hashtable集合、Collections工具类和模拟斗地主洗牌和发牌
1.Map集合概述和特点 * A:Map接口概述 * 查看API可以知道: * 将键映射到值的对象 * 一个映射不能包含重复的键 * 每个键最多 ...