关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳
#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; } };