会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
业精于勤荒于嬉,形成于思毁于随
订阅
管理
4.5.1简单递归消除
//
DataStructTest.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
iostream.h
>
#include
<
malloc.h
>
//
递归方法
int
Fib_1(
int
n)
{
if
(n
==
0
)
return
0
;
else
if
(n
==
1
)
return
1
;
else
return
Fib_1(n
-
1
)
+
Fib_1(n
-
2
);
}
//
循环方法
int
Fib_2(
int
n)
{
if
(n
==
0
)
return
0
;
else
{
int
x
=
0
,y
=
1
,z
=
0
;
for
(
int
i
=
2
;i
<=
n;i
++
)
{
z
=
y;
y
=
x
+
y;
x
=
z;
}
return
y;
}
}
int
main(
int
argc,
char
*
argv[])
{
cout
<<
"
递方方法计算N=20的斐波那齐数列
"
<<
Fib_1(
20
)
<<
endl;
//
cout
<<
"
循环方法计算N=20的斐波那齐数列
"
<<
Fib_2(
20
)
<<
endl;
//
return
0
;
}
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。
posted @
2007-07-12 16:17
吴东雷
阅读(
245
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告