递归函数的问题

chinese_ys 2006-11-09 02:04:26
下面是一段很简单的递归代码,但是为什麽我无法得到正确的结果?不论输入什麽,结果都是输出0。不会和编译系统有关吧?我在linux下编译的。

高人帮忙看一下,谢谢!
#include <iostream>
using namespace std;

int findmultipleoftwo(int);
int main()
{
int input,answer;
cout<<"Enter some integer: ";
cin>>input;
answer=findmultipleoftwo(input);
cout<<input<<" can be divide by two "<<answer<<" times!"<<endl;
return 0;
}
int findmultipleoftwo(int a)
{
static int count;
a=a/2;
count++;
if(a>2)
{
findmultipleoftwo(a);
}
else
{
return count;
}
}
...全文
285 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinese_ys 2006-11-10
  • 打赏
  • 举报
回复
都是怎末了,xp下编译又出结果了?
到底哪个是标准啊!
chinese_ys 2006-11-09
  • 打赏
  • 举报
回复
是我忘了说了,我要实现菲博那且数列。
我是在linux下编译的阿
如果不用static的话,那我怎末用count在递归里计数呢,不就实现不了那个数列了吗
nule 2006-11-09
  • 打赏
  • 举报
回复
Enter some integer: 50
50 can be divide by two 10 times!

不知道你的编译环境是什么样的,我在windows XP +MinGW下编译的结果如上。
guochun 2006-11-09
  • 打赏
  • 举报
回复
你要实现什么功能,你这没什么意义,递归是靠栈来实现的,不应该出现static类变量。可以这样,你试一下:
int findmultipleoftwo(int a)
{
a=a/2;
if(a>2)//修改
{
return 1+findmultipleoftwo(a);
}
else
{
return 1;
}
}
结果应该是你想要的
hudinho 2006-11-09
  • 打赏
  • 举报
回复
我有Windows xp
C-Free 3.5下编译lz的源码~
有如下结果! :
50 can be divide by two 5 times!
100 can be divide by two 6 times!
2 can be divide by two 1 times!


qwerasd1234 2006-11-09
  • 打赏
  • 举报
回复
这是我的修改,请指教,用vc
#include <iostream>
using namespace std;

int findmultipleoftwo(int);
int main()
{
int input,answer;
cout<<"Enter some integer: ";
cin>>input;
answer=findmultipleoftwo(input);
cout<<input<<" can be divide by two "<<answer<<" times!"<<endl;
return 0;
}
int findmultipleoftwo(int a)
{
static int count = 0;//修改
a=a/2;
count++;
if(a>=2)//修改
{
findmultipleoftwo(a);
}
else
{
return count;
}
}
guochun 2006-11-09
  • 打赏
  • 举报
回复
向我上面所说answer=findmultipleoftwo(input);当input 50
返回的是answer=findmultipleoftwo(50);是第一次调用的结果
但你的return在
if(a>2)
{
findmultipleoftwo(a);
}
else
{
return count;
}
只有最后一次返回值给findmultipleoftwo(3);
findmultipleoftwo(50)没有任何返回值,可改为
int findmultipleoftwo(int a)
{
static int count;
a=a/2;
count++;
if(a>2)
{
findmultipleoftwo(a);
}
return count;
}
guochun 2006-11-09
  • 打赏
  • 举报
回复
你可以好好看一下递归的问题,主要了解一下递归栈问题
return 1+findmultipleoftwo(a);
首先a=50 调用findmultipleoftwo(50);
因50/2=25 返回1+findmultipleoftwo(25);
findmultipleoftwo(25)返回1+findmultipleoftwo(12);
findmultipleoftwo(12)返回1+findmultipleoftwo(6);
findmultipleoftwo(6)返回1+findmultipleoftwo(3);
findmultipleoftwo(3)返回1+findmultipleoftwo(1);
findmultipleoftwo(1)得到结果1,依次出栈的
findmultipleoftwo(3)=1+1=2
findmultipleoftwo(6)=1+2=3
findmultipleoftwo(12)=1+3=4
findmultipleoftwo(25)=1+4=5
findmultipleoftwo(50)=1+5=6
可能只会有错,但过程就是这样
iambic 2006-11-09
  • 打赏
  • 举报
回复
这是想算啥啊?
lyy1089 2006-11-09
  • 打赏
  • 举报
回复
楼上,正确的结果是50 can be divide by two 25 times!
chinese_ys 2006-11-09
  • 打赏
  • 举报
回复
qwerasd1234()
你的方法有问题,你试过马?

guochun(yingc)
你的方法可以,但能给我讲一下为什麽马?count已经在递归里自增了阿

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧