您的位置:首页 > 其它

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
1.00
3.71
0.04
5.19
0.00

SampleOutput
3card(s)
61card(s)
1card(s)
273card(s)
简单的归结就是一个级数求和的问题

#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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: