longvatrong111
Senior Member
Có cách này đến n tầm 100 vẫn đúngNay chả có gì làm nên code C++ để chạy cho nhanh
View attachment 1248918
https://leetcode.com/submissions/detail/739569272/
C++:class Solution { public: int fibo[31] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040 }; int fib(int n) { return fibo[n]; } };
C++:
class Solution {
public:
int fib(int& n) {
if (n == 0) return 0;
return n < 30 ? (pow(1.6180339,n) - pow(0-0.6180339,n))/2.236067977 + 1 : (pow(1.6180339,n) - pow(0-0.6180339,n))/2.236067977 + 2;
}
};