这条算法怎么算了?

吴青峰 2008-05-28 03:32:18
也就是说:如输入8的话,在控制台的输出结果:1 1 2 3 5 8
也就是说:如输入15的话,在控制台的输出结果:1 1 2 3 5 8 13
也就是说:如输入16的话,在控制台的输出结果:1 1 2 3 5 8 13
也就是说:如输入80的话,在控制台的输出结果:1 1 2 3 5 8 13 21 34 55
就这样

我不记得怎么输了,求教专业人士。
...全文
122 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
laorer 2008-05-28
  • 打赏
  • 举报
回复
没事,我错了,随后写的,没有测试,所以肯定有问题
jason_kou 2008-05-28
  • 打赏
  • 举报
回复
偶发现偶很不厚道,偷了人家的东西,还比别人多拿了分。。
laorer 2008-05-28
  • 打赏
  • 举报
回复
.......................
gongyali2005 2008-05-28
  • 打赏
  • 举报
回复
斐波那契数列
找找规律,这个比较简单.代码网上多的很
hoszone 2008-05-28
  • 打赏
  • 举报
回复
void test(int a){
int i=1;
int temp =1;
do{
System.out.print(temp+" ");
System.out.print(i+" ");
if(i+temp <a ){
temp =i+temp;
}
i = temp+i;
}while(i <= a);
}
nisersent 2008-05-28
  • 打赏
  • 举报
回复
没注意到2楼正解,丢脸,下会不来了。。。
nisersent 2008-05-28
  • 打赏
  • 举报
回复

public int fun1(int num){
if(num == 1 ¦ ¦ num == 2){
num = 1;
}else{
return fun(num - 1) + fun(num - 2);
}
}

public void fun2(int num){
int temp = 0;
for(int i = 1; i <= num; i++){
temp = fun(i);
if(temp >= num) break;//终止循环
System.out.println(temp);
}
}

public void main(String[] args){
fun2(15);
}
/*执行过程
num = 15;

i = 1;
temp = 1;

i = 2;
temp = 1;

i = 3;
temp = 2;

i = 4;
temp = 3;

i = 5;
temp = 5;

i = 6;
temp = 8;

i = 7;
temp = 13;

i = 8;
temp = 21;temp >= num成立,退出循环。

打印结果:
1
1
2
3
5
8
13
*/
上面的看着不舒服,再来一个。
nisersent 2008-05-28
  • 打赏
  • 举报
回复
public int fun1(int num){
if(num == 1 || num == 2){
num = 1;
}else{
return fun(num - 1) + fun(num - 2);
}
}

public void fun2(int num){
int temp = 0;
for(int i = 1; i <= num; i++){
temp = fun(i);
if(temp >= num) break;//终止循环
System.out.println(temp);
}
}

public void main(String[] args){
fun2(15);
}
/*执行过程
num = 15;

i = 1;
temp = 1;

i = 2;
temp = 1;

i = 3;
temp = 2;

i = 4;
temp = 3;

i = 5;
temp = 5;

i = 6;
temp = 8;

i = 7;
temp = 13;

i = 8;
temp = 21;temp >= num成立,退出循环。

打印结果:
1
1
2
3
5
8
13
*/
想了好久,搞定!
jason_kou 2008-05-28
  • 打赏
  • 举报
回复
void fun(int t) {
int r1 = 0, r2 = 1,temp=0;
if (t < 0)
return;
while (r2 <= t) {
System.out.print(r2 + "\t");
temp=r1;
r1=r2;
r2 = temp+r2;
}
lisl2003 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qingfeng_wu 的回复:]
我的意思是说,我随便输入一个数,它都会帮我这样输出:后一个数等于前两个数之和。
[/Quote]
问题是什么?

java程序如何读取我们在控制台上的输入?
吴青峰 2008-05-28
  • 打赏
  • 举报
回复
我的意思是说,我随便输入一个数,它都会帮我这样输出:后一个数等于前两个数之和。
laorer 2008-05-28
  • 打赏
  • 举报
回复
。。。。。
void fun(int t){
int r1=1,r2=0;
if(t<0)
return;
while (r1<=t){
System.out.print(r1+"\t");
r1=r1+r2;
r2 = r1;
}
}
lisl2003 2008-05-28
  • 打赏
  • 举报
回复
1 1 2 3 5 …… n m m+n
每个数都是前面两个数的和,最在的那个数不能超过你输入的数。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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