PAT 1176 The Closest Fibonacci Number
作者:
NikoNairre
,
2023-11-29 09:14:08
,
所有人可见
,
阅读 86
C++
#include <iostream>
#include <vector>
using namespace std;
vector<int> fib;
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
fib.push_back(0), fib.push_back(1);
while (fib.back() <= 1e8) {
int b1 = fib[fib.size() - 1];
int b2 = fib[fib.size() - 2];
fib.push_back(b1 + b2);
}
int k; cin >> k;
int dif = 0x3f3f3f3f;
int res = -1;
for (int i = 0; i < fib.size(); i++ ) {
int cdif = abs(fib[i] - k);
if (dif > cdif) {
dif = cdif;
res = fib[i];
}
}
cout << res;
}