您的位置:首页 > 其它

ZOJ1045

2016-01-04 19:34 267 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1045

第一感觉,二分。因为不知道上限是多少,就试一下5.2需要多少长卡片,276张,于是用276作为上限来二分(这么少直接暴力就可以的>_<!)

#include<iostream>
#include<cstdio>

using namespace std;

bool OK(int n,float length)
{
float sum = 0;
for (int i=1; i<=n; i++)
{
sum += 1.0/(i+1);
if (sum >= length)
return true;
}
return false;
}

int main()
{
float length;

while (cin>>length && length != 0)
{
int head = 1, tail = 277;
int mid,ans;
while (head<tail)
{
mid = (head+tail)>>1;
if (OK(mid,length))
{
ans = mid;
tail = mid;
}
else
head = mid+1;
}
cout<<ans<<" card(s)"<<endl;
}

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