pku oj overhang叠加卡片求最少的卡片数
2016-04-28 21:44
357 查看
这个估计是里面第二简单的了,因为第一简单的是求a+b
哈哈,一submit就ac了
题目如下:
DescriptionHowfarcanyoumakeastackofcardsoverhangatable?Ifyouhaveonecard,youcancreateamaximumoverhangofhalfacardlength.(We'reassumingthatthecardsmustbeperpendiculartothetable.)Withtwocardsyoucanmakethetopcardoverhangthebottomonebyhalfacardlength,andthebottomoneoverhangthetablebyathirdofacardlength,foratotalmaximumoverhangof1/2+1/3=5/6cardlengths.Ingeneralyoucanmakencardsoverhangby1/2+1/3+1/4+...+1/(n+1)cardlengths,wherethetopcardoverhangsthesecondby1/2,thesecondoverhangsthathirdby1/3,thethirdoverhangsthefourthby1/4,etc.,andthebottomcardoverhangsthetableby1/(n+1).Thisisillustratedinthefigurebelow.
InputTheinputconsistsofoneormoretestcases,followedbyalinecontainingthenumber0.00thatsignalstheendoftheinput.Eachtestcaseisasinglelinecontainingapositivefloating-pointnumbercwhosevalueisatleast0.01andatmost5.20;cwillcontainexactlythreedigits.OutputForeachtestcase,outputtheminimumnumberofcardsnecessarytoachieveanoverhangofatleastccardlengths.Usetheexactoutputformatshownintheexamples.SampleInput
SampleOutput
#include<iostream>
usingnamespacestd;
classOverhang{
private:
doublea;
public:
intgetCardsNum();
voidgetValue(){cin>>a;}
doublegeta(){returna;}
};
intOverhang::getCardsNum(){
intnum=1;
doublesum=0.0;
while(true){
sum+=1.0/(num+1);
num++;
if(sum>=a)
return--num;
}
}
intmain(void){
Overhangt;
while(true){
t.getValue();
if((t.geta()-0.0)<0.0001)
return0;
cout<<t.getCardsNum()<<"card(s)"<<endl;
}
}
哈哈,一submit就ac了
题目如下:
DescriptionHowfarcanyoumakeastackofcardsoverhangatable?Ifyouhaveonecard,youcancreateamaximumoverhangofhalfacardlength.(We'reassumingthatthecardsmustbeperpendiculartothetable.)Withtwocardsyoucanmakethetopcardoverhangthebottomonebyhalfacardlength,andthebottomoneoverhangthetablebyathirdofacardlength,foratotalmaximumoverhangof1/2+1/3=5/6cardlengths.Ingeneralyoucanmakencardsoverhangby1/2+1/3+1/4+...+1/(n+1)cardlengths,wherethetopcardoverhangsthesecondby1/2,thesecondoverhangsthathirdby1/3,thethirdoverhangsthefourthby1/4,etc.,andthebottomcardoverhangsthetableby1/(n+1).Thisisillustratedinthefigurebelow.
InputTheinputconsistsofoneormoretestcases,followedbyalinecontainingthenumber0.00thatsignalstheendoftheinput.Eachtestcaseisasinglelinecontainingapositivefloating-pointnumbercwhosevalueisatleast0.01andatmost5.20;cwillcontainexactlythreedigits.OutputForeachtestcase,outputtheminimumnumberofcardsnecessarytoachieveanoverhangofatleastccardlengths.Usetheexactoutputformatshownintheexamples.SampleInput
1.00 3.71 0.04 5.19 0.00
SampleOutput
3card(s) 61card(s) 1card(s) 273card(s)
简单的归结就是一个级数求和的问题
usingnamespacestd;
classOverhang{
private:
doublea;
public:
intgetCardsNum();
voidgetValue(){cin>>a;}
doublegeta(){returna;}
};
intOverhang::getCardsNum(){
intnum=1;
doublesum=0.0;
while(true){
sum+=1.0/(num+1);
num++;
if(sum>=a)
return--num;
}
}
intmain(void){
Overhangt;
while(true){
t.getValue();
if((t.geta()-0.0)<0.0001)
return0;
cout<<t.getCardsNum()<<"card(s)"<<endl;
}
}
相关文章推荐
- ngx_http_process_request_line函数解析
- 复习封装,继承,多态
- JavaScript与Objective-C的互相调用
- mxnet深度学习(NDArray)
- Android系列之Activity
- Java 注解
- Shell脚本学习笔记(一)
- MySQL定时器开启、调用实现代码
- 常用APDU指令错误码
- xcode 插件
- 团队项目冲刺第十天
- LintCode:有效数字
- 机器学习实战 第九章 源码勘误
- TextKit/富文本/图层混排模式的使用(一)NSAttributedString
- 第十周 学习进度表
- maven 学习---使用Maven模板创建项目
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C 离散化+树状数组+map D 数学
- PAT 1008. 数组元素循环右移问题
- bloom filter
- Objective-C与js相互调用及传参数注意