ZOJ1045

题目链接: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;
}


你可能感兴趣的:(ZOJ1045)