注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

长风明志的博客

不要也不能做下一个谁,应该且可以做第一个自己

 
 
 

日志

 
 

【POJ 1003】 Hangover  

2013-03-27 14:42:40|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 87218Accepted: 42047

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.


【POJ 1003】 Hangover - changfengmingzhi - 长风明志的博客

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)

这也是一道极其简单的题,主要考查的是浮点数的迭代求和运算(个人写得代码不高效,请见谅):
源代码一:
220K  16MS

#include <iostream> #include <vector> #include <cmath> using namespace std; int main() { float c; vector<float> vecInput; while(cin>>c) { if(fabs(c-0.0)<=1e-6) { break; } vecInput.push_back(c); } for(int i=0;i<vecInput.size();i++) { double sum=0.0; int n=1; while(true) { sum+=1/(double)(n+1); if(sum>vecInput[i]||fabs(sum-vecInput[i])<=1e-6) { break; } n++; } cout<<n<<" card(s)"<<endl; } return 0; }

稍微修改了下后,时间相对高效了些,
源代码二:
216K  0Ms

#include <iostream> #include <vector> #include <cmath> using namespace std; int main() { float c; vector<int> vecOutput; double sum; int n; while(cin>>c) { if(fabs(c-0.0)<=1e-6) { break; } sum=0.0; n=1; while(true) { sum+=1/(double)(n+1); if(sum>c||fabs(sum-c)<=1e-6) { break; } n++; } vecOutput.push_back(n); } for(int i=0;i<vecOutput.size();i++) { cout<<vecOutput[i]<<" card(s)"<<endl; } return 0; }

  评论这张
 
阅读(616)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018