C++编程:用递归法计算斐波那契数列第n项的值(同时输出前n项)-请修改。
#include<iostream>usingnamespacestd;intfibonacci(intn){intfibo;staticinttemp;if(n==1|...
#include<iostream>
using namespace std;
int fibonacci(int n)
{
int fibo;
static int temp;
if (n == 1 || n == 2)
fibo = 1;
else
{
fibo = ————?;
if (temp < fibo)
{
cout << " " << fibo;
temp = fibo;
}
}
return ————?;
}
int main()
{
int n, fibon;
cout << "Please input a positive integer:";
cin >> n;
cout << "The " << n << " number of fibonacci is:\n [ 1 1";
fibon = fibonacci(n);
cout << " ]\n " << fibon << endl;
system("pause");
}
自学C++中,上面两空实在想不出来填啥,希望得到帮助,谢谢 展开
using namespace std;
int fibonacci(int n)
{
int fibo;
static int temp;
if (n == 1 || n == 2)
fibo = 1;
else
{
fibo = ————?;
if (temp < fibo)
{
cout << " " << fibo;
temp = fibo;
}
}
return ————?;
}
int main()
{
int n, fibon;
cout << "Please input a positive integer:";
cin >> n;
cout << "The " << n << " number of fibonacci is:\n [ 1 1";
fibon = fibonacci(n);
cout << " ]\n " << fibon << endl;
system("pause");
}
自学C++中,上面两空实在想不出来填啥,希望得到帮助,谢谢 展开
1个回答
2018-08-27
展开全部
#include<iostream>
using namespace std;
int fibonacci(int n){
int fibo;
static int temp;
if 厅丛配(n == 1 || n == 2)
fibo = 1;
else{
fibo = fibonacci(n-1) + fibonacci(n-2);
if (temp < fibo){
cout << " " << fibo;
temp = fibo;
}
}
return fibo;
}
int main(){
int n, 扮指fibon;
cout << "Please input a positive integer:";
cin >> n;
cout << "The " << n << " number of fibonacci is:\n [ 1 1";
fibon = fibonacci(n);
cout << 郑闷" ]\n " << fibon << endl;
system("pause");
}
应该是这样,按公式(f(n)=f(n-1)+f(n-2)).
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询