c++程序,c++ Fibonacci数列
展开全部
#include "iostream"
using namespace std;
unsigned long long data[100] = {0, 1, 2, 0};
unsigned long long fib(int n) {
int i;
if (n == 0) return 0;
if (data[n]) return data[n];
for (i = 3; i <=n; i++) {
if (data[i] == 0) {
data[i] = data[i-1] + data[i-2];
}
}
return data[n];
}
int main() {
int n;
while (1) {
cin >> n;
cout << "Fib " << n << " is " << fib(n) << endl;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询