topcoder SRM 522 DIV2 FibonacciDiv2

时间:2014-06-06 13:18:11   收藏:0   阅读:305

关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳

bubuko.com,布布扣
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class FibonacciDiv2{
public:
    vector<int> table;
    void make_table(){
        table.push_back(0);
        table.push_back(1);
        int newData = 0;
        do{
             int last = table.size()-1;
            newData = table[last]+table[last-1];
            table.push_back(newData);
        }while(newData <= 1000000);
    }
    int find(int N){
        make_table();
        int len = table.size(), result=0;
        if(N <= table[0]) result =  table[0]-N;
        else if(N >= table[len-1]) result= N-table[len-1];
        else{
            for(int i = 0 ; i< len-1; ++ i){
                if( table[i+1]>=N && table[i]<= N ){ result = min(table[i+1]-N,N-table[i]);break;}
            }
        }
        return result;
    }
};
bubuko.com,布布扣

 

 

topcoder SRM 522 DIV2 FibonacciDiv2,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!