pku oj overhang叠加卡片求最少的卡片数

news/2024/7/7 20:58:56

这个估计是里面第二简单的了,因为第一简单的是求a+b

哈哈,一submit就ac了

题目如下:

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.

hangover.jpg

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)
 
简单的归结就是一个级数求和的问题
#include <iostream>
using namespace std;

class Overhang {
private:
    double a;
public:
    int getCardsNum();
    void getValue() { cin >> a; }
    double geta() { return a; }
};

int Overhang::getCardsNum() {
    int num=1;
    double sum=0.0;
    while(true) {
        sum += 1.0/(num+1);
        num++;
        if (sum >= a)
            return --num;
    }
}
int main(void) {
    Overhang t;
    while(true) {
        t.getValue();
        if ((t.geta() -  0.0) < 0.0001)
            return 0;
        cout << t.getCardsNum() << " card(s)" << endl; 
    }
}

http://www.niftyadmin.cn/n/2211976.html

相关文章

python小demo之判断闰年

#_*_ codingutf-8 __author__ kysida #定义函数 def leap_year(year):if year % 4 0:if (year / 100) % 4 0:return "the year is a leap year"return "the year is not a leap year" #主程序 if __name__ __main__:year int(raw_input("Please …

用mysql工具导数据_MySQL导入导出数据mysqldump工具的基本用法及mysql的备份总结

导出要用到MySQL的mysqldump工具&#xff0c;基本用法是&#xff1a;shell> mysqldump [OPTIONS] database [tables]如果你不给定任何表&#xff0c;整个数据库将被导出。通过执行mysqldump --help&#xff0c;你能得到你mysqldump的版本支持的选项表。注意&#xff1a;如果…

转 Linux命令-文件管理命令

http://jingyan.baidu.com/article/9113f81bc1c7a72b3214c7d3.html Linux命令-文件管理命令 浏览&#xff1a;4118|更新&#xff1a;2012-11-12 15:26|标签&#xff1a;linux linux系统因其优秀的稳定性和安全性&#xff0c;被越来越多的企业服务器应用。随之而来的越来越多的人…

python 基础知识梳理——迭代器和生成器

引言 for i in [1,2,3,4,5,6]:print(i)这是一个非常简单的for in语句&#xff0c;什么样的对象可以被用来迭代呢&#xff1f; 容器、可迭代对象、迭代器 在Python中一切皆是对象&#xff0c;对象的抽象就是类&#xff0c;而对象的集合就是容器。 列表list:[0,1,2]&#xff…

02-、java01-File类、递归

01、这是进阶java的博客总结&#xff0c;有各种课程的学习总结和代码总结&#xff0c;以后再做一个自己的工具箱博客总结&#xff0c;希望大家努力学习加油呀 02、File类 --除了 java.lang[主要是各种基本类型&#xff0c;不需要导入] java.util[各种基础工具&#xff0c;需要…

mysql shell 8.0.11_MySQL.8.0.11安装

环境&#xff1a;Win7x64、Win10x64、MySQL 8.0.15(下载于官方网站)ZC&#xff1a;下面&#xff0c;第10&#xff0c;有自己机子上安装的过程&#xff0c;简单一些&#xff0c;直接无脑照着做就行ZC&#xff1a;步骤还是比较少的&#xff0c;还蛮清晰的。主要的就是&#xff1a…

做好一个网站其实不是单单靠时间积累那么简单.

有些竞争对手站点收录量、外链量都没有小编的多&#xff0c;可他的权重、独立IP就非常可观&#xff0c;这时候有些人可能会有“难道外链真的不再需要”的想法&#xff0c;但这是完全错误的理解。你再仔细一点研究对方站点的外链&#xff0c;你会发现原来是因为“外链的质量要好…

python 基础知识梳理——函数

python 基础知识梳理——函数 函数基础 上来先看一个简单的例子吧 def sum_numbers(a,b):return ab result sum_numbers(3,5) print(result) # 输出 8在调用函数时&#xff0c;必须在函数调用前声明函数&#xff0c;不然的话可能会报错。 result sum_numbers(3,5) def su…