注:文章内容来源于网络,真实性有待确认,请自行甄别。
C++高手来下,想请教下递归~~#include<iost
发表于:2024-10-24 00:00:00浏览:16次
问题描述:#include <tream>
int fioo (int n);
int main()
{
using std::cout;
int n, answer;
cout << "Enter number to find:";
std::cin >>n;
cout << "\n\n";
answer =fioo(n);
cout << answer << "is the" << n;
cout << " th Fib#include <tream>
int fioo (int n);
int main()
{
using std::cout;
int n, answer;
cout << "Enter number to find:";
std::cin >>n;
cout << "\n\n";
answer =fioo(n);
cout << answer << "is the" << n;
cout << " th Fibonacci number \n";
return 0;
}
int fioo (int n)
{
using std::cout;
cout << "Processing fioo(" << n <<")...";
if (n<3)
{
cout << "Return1!\n";
return (1);
}
else
{
cout << "Call fioo(" << n-2 << ")";
cout << "and fioo(" << n-1 << ").\n";
return (fioo(n-2)+fioo(n-1));
}
}
我还是新手 刚学到递归 看的一头雾水啊``我实在找不出他为什么会调用自己的地方,谁能告诉我是哪一句代码吗?``
还有倒数第4、5句的
cout << "Call fioo(" << n-2 << ")";
cout << "and fioo(" << n-1 << ").\n";
我输入6 运行以后输出
Call fioo(4)and fioo(5).
为什么6-2以后不是4-1 而又从6-1了呢```
最后想问的是`他怎么会不返回啊``fioo函数的2个return 都没有直接返回answer 并输出,而是不断细分
最后居然还把得到的1都加起来了才返回给answer了``实在不能理解啊~~高手指教下>_<
这算什么递归啊,把n赋值为6,n-2=4,n-1=5肯定很正常啊,n-2=4,又不是n等于4
没时间用c给你写了一个,至于你的第一个和第三个问题没看明白,请说清楚点发我邮件吧
int fun(int a)
{
int b;
b += a;
return(b);
}
void main(void)
{
int sum = 1;
sun += fun(sum);
}
栏目分类全部>