[POJ] #1003# Hangover : 浮点数运算

一. 题目

Hangover

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 116593   Accepted: 56886

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

Mid-Central USA 2001

二. 题意

  • 按照固定的累加方式:从高层到低层,相对于其相邻下层可以伸出的长度为(1/2),(1/3),...,(1/n)
  • 给定一个指定长度 S,问最少需要累加多上块板,使其相对于桌面的伸出长度大于或等于 S

三. 分析

  • 算法核心: 此题比较简单,无需考虑任何算法
  • 实现细节: 简单的浮点数累加运算即可

四. 题解

 1 #include <stdio.h>
 2
 3 int main()
 4 {
 5     int i;
 6     float length, sum;
 7
 8     while (1) {
 9         sum = 0;
10         scanf("%f\n", &length);
11         if (!length) break;
12
13         for (i = 2; ; i++) {
14             sum += (1 / (float)i);
15
16             if (sum >= length) {
17                 printf("%d card(s)\n", i - 1);
18                 break;
19             }
20         }
21     }
22
23     return 0;
24 }
时间: 2024-08-01 20:13:31

[POJ] #1003# Hangover : 浮点数运算的相关文章

poj 1003:Hangover(水题,数学模拟)

Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 99450   Accepted: 48213 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 as

POJ 1003 Hangover&amp;&amp;NYOJ 156 Hangover【数学题】

计算1+1/2+1/3+++1/n Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 104558   Accepted: 50926 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 car

poj 1003 Hangover

#include <iostream> using namespace std; int main() { double len; while(cin >> len && len) { double sum = 0.0; double i = 1.0; int n = 2; while(sum < len) { sum += i/n; ++n; } cout << n-2 << " card(s)" <<

【javascript】浮点数运算问题分析及解决方法

问题: 在用 js 进行小数四则运算时发现了一个重大问题,比如:0.7 * 0.8 = 0.5599999999999999 分析: 在 js 中只有一种数字类型 Number,而且在 js 中所有的数字都是以 IEEE-754 标准格式表示的.浮点数的精度问题并不是 js 特有的,因为有些小数以二进制表示位数是无穷的,比如 1.1,其程序实际上无法真正的表示 1.1,而只能做到一定程度上的准确(1.09999999999999999),这是无法避免的精度丢失. 通过 chrome 控制台,我们

程序员必知之浮点数运算原理详解

导读:浮点数运算是一个非常有技术含量的话题,不太容易掌握.许多程序员都不清楚使用==操作符比较float/double类型的话到底出现什么问题. 许多人使用float/double进行货币计算时经常会犯错.这篇文章是这一系列中的精华,所有的软件开发人员都应该读一下. 随着你经验的增长,你肯定 想去深入了解一些常见的东西的细节,浮点数运算就是其中之一. 1. 什么是浮点数? 在计算机系统的发展过程中,曾经提出过多种方法表达实数. [1]典型的比如相对于浮点数的定点数(Fixed Point Num

POJ Exponentiation(大浮点数JAVA轻松解决)

题目链接:Clicke Here~ java解决大数就是爽阿!~ 以前大数模板敲啊敲的,敲了半天发现一交果断wrong.只从学会了java妈妈在不用担心我遇到大数了/ 这道题遇到的Java函数有: stripTrailingZeros()            去掉后缀0 toPlainString()               返回大数的非科学计数法 startsWith() startsWith()函数介绍及扩展: 描述 startsWith(),endsWith()的作用,用法,判断字符

记C语言浮点数运算处理 "坑" 一则

看一小段C语言程序: int main() { float x = 1.3; x = x - (int)x; int i = (int)(x*10); return 0; } 在你心目中, 变量 I 是怎样的结果? 如果你理所当然地认为是3的话, 那么你就错了~~~ 实际结果应该是2.   为什么? 简而言之, x在内存的值并不是精确的1.3, 实际上可能是1.29999999...... 因为在计算机组成原因中有说过, 浮点数无法被准确地表示出来, 只能是一个非常精确的值.. 就算现在你已经知

js,java,浮点数运算错误及应对方法

js,java,浮点数运算错误及应对方法 一,浮点数为什么会有运算错误 IEEE 754 标准规定了计算机程序设计环境中的二进制和十进制的浮点数自述的交换.算术格式以及方法. 现有存储介质都是2进制.2进制的进制基数是2,那么一个数字只要被因素包含大于2的质数的数除,都会产生无限循环小数.无限循环小数和无理数都无法,和非无限循环的有理数一起用同一种方式存储到存储介质上的同时还保持计算的兼容性. 对于无限循环小数,可以设计一种格式存储到介质上,但是同时又要和非无限循环的有理数能够计算,效率应该会变

【转】javascript 浮点数运算问题

大多数语言在处理浮点数的时候都会遇到精度问题,但是在JS里似乎特别严重,来看一个例子 alert(45.6*13); 结果居然是592.800000000001,当然加法之类的也会有这个问题 那这是js的错误吗? 当然不是,你的电脑做着正确的二进制浮点运算,但问题是你输入的是十进制的数,电脑以二进制运算,这两者并不是总是转化那么好的,有时候会得到正确的结果,但有时候就不那么幸运了 alert(0.7+0.1);//输出0.7999999999999999 alert(0.6+0.2);//输出0