c++求指定范围内fibonacci
时间: 2024-02-19 17:54:25 浏览: 18
以下是C++求指定范围内Fibonacci数的代码实现:
```cpp
#include <iostream>
using namespace std;
int fib(int n) {
int x1 = 1, x2 = 1, x = 0;
if (n <= 2) {
return 1;
} else {
for (int i = 3; i <= n; i++) {
x = x1 + x2;
x1 = x2;
x2 = x;
}
return x;
}
}
void PrintFN(int m, int n) {
int count = 0, i = 1;
while (fib(i) <= n) {
if (fib(i) >= m) {
count++;
if (count == 1) {
cout << "fib(" << i << ") = " << fib(i);
} else {
cout << " " << fib(i);
}
}
i++;
}
if (count == 0) {
cout << "No Fibonacci number";
}
}
int main() {
int m, n;
cin >> m >> n;
PrintFN(m, n);
return 0;
}
```