这是个麻烦的序列,将如何实现?

beeway 2009-11-28 03:31:20
/*
有一个序列,首两项为0,1。以后各项值为前两项之和,写一个方
法来实现求在1000以内这个序列并求和?
*/
请问,就我现在这个程序,如果要改的话,要怎样改才好呢?
public class A2{
public int t1(int a,int b,int sum){
if(a==0&b==1){
sum=a+b;
int tempB=a+b;
a=b;
b=tempB;

System.out.println(sum);
}else{
int tempB=a+b;


a=b;
b=tempB;
sum=a+b;
if(tempB>10){
return sum;
}
sum=sum+tempB;
System.out.println(sum);
}
sum=t1(a,b,sum);
return sum ;
}

public static void main(String[] args){
A2 t = new A2();
t.t1(0,1,0);

}
...全文
185 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
joewa718 2009-11-30
  • 打赏
  • 举报
回复
class TestMain
{
public static void main(String[] args)
{
long sum=0;
long [] a=new long[1000];
a[0]=0;
a[1]=1;
for(int i=2;i<=1000-1;i++)
{
a[i]=a[i-1]+a[i-2];
}
for(int i=0;i<=1000-1;i++)
{
sum+=sum+a[i];
}
for(int j=0;j<=1000-1;j++)
{
System.out.print(a[j]+" ");
}
System.out.println("总大小"+sum);
}
}写了一个我认为比较简单的
一头头 2009-11-29
  • 打赏
  • 举报
回复
recursion 递归 刚学这个词
godismydaughter 2009-11-29
  • 打赏
  • 举报
回复
实现求在1000以内这个序列并求和?? 这个我不明白是什么意思。

public static void main(String[] args) {
// TODO Auto-generated method stub
get(40);
}

static void get(int n){
int a[]=new int[n];
a[0]=0;
a[1]=1;
System.out.print("\t"+a[0]+"\t"+a[1]);
int sum=1;
for(int i=2;i<n;i++){
a[i]=a[i-1]+a[i-2];
System.out.print("\t"+a[i]);
sum+=a[i];
if(i%10==0){
System.out.println();
}
if(a[i]>1000){
break;
}
}
System.out.println();
System.out.println(sum);
}
这是我想的方法,不知合不合题意。
zx8813443 2009-11-29
  • 打赏
  • 举报
回复

public class Test1 {

public static void main(String[] args){

Test1 t = new Test1();
System.out.println(Test1.feibonaci(10));

}
public static int feibonaci(int n)
{
int sum=0,temp=0;
int N=n;
int a=0,b=1;
if(n<=2)
{
sum=a+b;
System.out.print(a+","+b+",");
return sum;
}
else
{
while(n-2!=0)//应该是减2
{
if(n==N)
{
temp=a+b;
sum+=temp+a+b;
System.out.print(a+",");
a=b;
b=temp;
System.out.print(a+","+b+",");
n--;
continue;
}
temp=a+b;
sum+=temp;
System.out.print(temp+",");
a=b;
b=temp;
n--;
}
}
return sum;
}
}
zx8813443 2009-11-29
  • 打赏
  • 举报
回复

public class Test1 {

public static void main(String[] args){

Test1 t = new Test1();
System.out.println(Test1.feibonaci(10));

}
public static int feibonaci(int n)
{
int sum=0,temp=0;
int N=n;
int a=0,b=1;
if(n<=2)
{
sum=a+b;
System.out.print(a+","+b+",");
return sum;
}
else
{
while(n-1!=0)
{
if(n==N)
{
temp=a+b;
sum+=temp+a+b;
System.out.print(a+",");
a=b;
b=temp;
System.out.print(a+","+b+",");
n--;
continue;
}
temp=a+b;
sum+=temp;
System.out.print(temp+",");
a=b;
b=temp;
n--;
}
}
return sum;
}


}
秦π 2009-11-28
  • 打赏
  • 举报
回复
这种就递归吧
tg008007x3 2009-11-28
  • 打赏
  • 举报
回复
写了个实现非波那契数列求和的非递归算法方法,思想和大家都差不多。祝楼主好运。
public static int test1(int count)//
{
int a=0;
int b=1;
int sum=0;
int temp;
while(true)
{
if(count==1)
{
break;

}
sum+=b;
temp=a;
a=b;
b=b+temp;
count--;
}
return sum;


}
beeway 2009-11-28
  • 打赏
  • 举报
回复
我刚把这个序列修改好了。
public class A2{
public int t1(int a,int b,int sum ){
if(a==0&b==1){
sum =a+b;
int tempB=a+b;
System.out.println(a);
System.out.println(b);
a=b;
b=tempB;

System.out.println(b);
}else{
int tempB=a+b;
//sum=a+b;
a=b;
b=tempB;
if(tempB>1000){
return sum;
}
System.out.println(b);
sum=sum+tempB;
}
return t1(a,b,sum);
}
public static void main(String args[]){
A2 t=new A2();
int a=t.t1(0,1,0);
a=a+1;
System.out.println(a);
}
}
z569362161 2009-11-28
  • 打赏
  • 举报
回复
非波那契数列

很典型的例题啊!

哪本教科书上都有啊!
baitf0930 2009-11-28
  • 打赏
  • 举报
回复
我写一个关于兔子的类似问题的解法,你看看吧:
import java.io.IOException;
public class CRabit {

int rabitCount(int count)
{
if(count <=1)
return 1;
else
return this.rabitCount(count-1)+this.rabitCount(count-2);
}
public static void main(String[] args) {

CRabit rabit = new CRabit();
try
{
System.out.println ("请输入要计算的月数:");
byte [] buffer = new byte[10];
System.in.read(buffer);
String str = new String(buffer).trim();
int count = Integer.parseInt(str);
for(int i=0;i<count;i++)
{
System.out.println("第"+(i+1)+"个月有"+rabit.rabitCount(i)+"+兔子");
}
}catch(IOException io)
{
System.out.println("输入错误!");
io.printStackTrace();
}


}
}
Dan1980 2009-11-28
  • 打赏
  • 举报
回复
搜索“非波那契数列”,一大堆。

62,621

社区成员

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

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