请教一下斐波那契数列的非递归算法

yunqibuhao 2011-10-21 12:17:29
如题
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhOu 2011-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mengmingtao 的回复:]
直接输出吗?
C/C++ code
#include <iostream>
using namespace std;

int main(){
int first = 0;
int second = 1;
int temp;
cout << first <<' ';
cout << second <<' ';
for(int i=0;i<10;i……
[/Quote]

#include <stdio.h>

int main(){
int first = 0;
int second = 1;
int temp;
int i;
printf("first\n");
printf("second\n");
for(i=0;i<10;i++){
temp = first + second;
first = second;
second = temp;
printf("%d\n",temp);
}
while(1);
}
这是我对2楼程序用c语言编写的内容
在dev c++环境下运行成功 你用tc或者vc可以把while(1)删掉
JieTouLangRen 2011-10-21
  • 打赏
  • 举报
回复
迭代~
mengmingtao 2011-10-21
  • 打赏
  • 举报
回复
直接输出吗?
#include <iostream>
using namespace std;

int main(){
int first = 0;
int second = 1;
int temp;
cout << first <<' ';
cout << second <<' ';
for(int i=0;i<10;i++){
temp = first + second;
first = second;
second = temp;
cout <<temp<<' ';
}
}
pathuang68 2011-10-21
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

int main(void)
{
int N = 0;
int first = 1;
int second = 1;
int temp;

cin >> N;

cout << "Result: " << endl;
cout << "F[1] = " << first << endl;
cout << "F[2] = " << second << endl;
for(int i = 2; i < N; i++)
{
temp = first + second;
first = second;
second = temp;
cout << "F[" << i + 1 << "] = " << temp << endl;
}

return 0;
}
zhOu 2011-10-21
  • 打赏
  • 举报
回复
还有你想显示到多少代 就把(代数—2)替换for循环中的10就可以了

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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