/*
有一个序列,首两项为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); }

解决方案 »

  1.   

    我写一个关于兔子的类似问题的解法,你看看吧:
    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();
         }
        
            
        }
    }
      

  2.   

    我刚把这个序列修改好了。
    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);
    }
    }
      

  3.   

    写了个实现非波那契数列求和的非递归算法方法,思想和大家都差不多。祝楼主好运。
    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;


    }
      

  4.   


    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;
       }
    }
      

  5.   


    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;
       }
    }
      

  6.   

    实现求在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);
        }
    这是我想的方法,不知合不合题意。
      

  7.   

    recursion 递归  刚学这个词
      

  8.   

    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);
    }
    }写了一个我认为比较简单的