POJ 1003 解题报告

1.问题描述:

http://poj.org/problem?id=1003

2.解题思路:

最直观的的想法是看能不能够直接求出一个通项式,然后直接算就好了, 但是这样好水的样子,而且也不知道这个通项式能否求出且精度如何?

题目中提到输入介于0.01 和5.20之间,可以确定的是最后求出来的卡片数肯定介于1-300之间,因此可以预先求出1-300张卡片分别垒起来可以达到的overhang长度。

然后用二分搜索来做。具体的代码如下:

/*
    author: obalama
    date: 2013.07.24
    email: areslipan@163.com
*/
#include <iostream>
#include <vector>
#include <iterator>
 
using namespace std;
 
int main()
{
    /*preprocessing*/
    double table[288];
 
    for(int i = 0;i<288; ++i)
    {
        if(i==0)table[i] = 1/2.0;
        else table[i] = table[i-1] + 1.0/(i+2);
    }
    
    vector<int> result;
 
    double cur;
    cin>> cur;
    while(cur-0>0.00001)
    {
        int head=0;
        int end = 277;
        int mid = (head + end)/2;
        while(table[mid] != cur)
        {
            if(table[mid] < cur)
            {
                head = mid +1;
            }
            else
            {
                end = mid -1;
            }
 
            if(head > end) break;
            else mid = (head + end)/2;
        }
 
        int maxNum;
        if(table[mid] >= cur)maxNum = mid +1;
        else
            maxNum = mid +2;
        result.push_back(maxNum);
        cin>> cur;
    }
 
    vector<int>::iterator iter;
    for(iter = result.begin(); iter != result.end(); ++iter)
    {
        cout<<*iter<<" card(s)"<<endl;
    }
 
    return 0;
}
 
 
原文地址:https://www.cnblogs.com/obama/p/3211816.html