两种不同的输出

时间:2016-12-27 06:32:02

标签: java

Fibonacci序列中的每个新术语都是通过添加前两个术语生成的。从和开始,前10个术语将是:

1,1,2,3,5,8,13,21,34,... 

在这里我们应该找到Fibonacci系列中的偶数并将它们加到总和

代码:

import java.util.*;

public class Abhi {


public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    int[] n = new int[t];

    int i,j;
    long sum;
    for(int a0 = 0; a0 < t; a0++){
        n[a0] = in.nextInt();            
    }
    //int an = n.length;
    int[] nn = new int[1000];
    nn[0]=1;
    nn[1]=2;
    for(i = 0 ; i<t;i++){
        sum = 2;
        for(j= 2;j<n[i];j++){                
            nn[j] = nn[j-2] + nn[j-1];
                if(nn[j]%2==0 && nn[j]<n[i])
                    {
                    sum += nn[j];
                 //System.out.println(sum);   
                 //the above line shows correct output 
                }
            }            
        System.out.println(sum);//this is printing different output
    }}}

示例输入:

1
100

示例输出:

44

3 个答案:

答案 0 :(得分:1)

这里的问题不在于外部的System.out.println(sum);如你所说。这是因为int范围。

int的最大值是2 147 483 647,而Fibonacci系列2 971 215 073位于第47位,当它超出int范围时,它以意想不到的方式给出结果。

在代码数组中nn持有-1323752223而不是2971215073,这实际上导致了问题。

要解决此问题,请使用BigInteger,如下所示

    BigInteger sum;
    BigInteger[] nn = new BigInteger[1000];
    nn[0] = new BigInteger("1");
    nn[1] = new BigInteger("2");
    for (i = 0; i < t; i++) {
        sum = new BigInteger("2");
        for (j = 2; j < n[i]; j++) {
            nn[j] = nn[j - 2].add(nn[j - 1]);
            if (nn[j].mod(new BigInteger("2")).equals(new BigInteger("0")) && 
                nn[j].compareTo(new BigInteger(String.valueOf(n[i])))<0) {
                sum = sum.add(nn[j]);
                System.out.println(sum);
            }
        }
        System.out.println(sum);
    }

答案 1 :(得分:0)

您还可以使用以下代码实现此目的:

import java.util.Scanner;

public class Fibo 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);

        int No = sc.nextInt();

        int fNo1 = 1;
        int fNo2 = 1;
        int fNo3 = fNo1 + fNo2;
        int sumofEvenNo = 0;
        int i;
        while( fNo3 < No)
        {
            if(fNo3 % 2 == 0)
                sumofEvenNo += fNo3;
            fNo1 = fNo2;
            fNo2 = fNo3;
            fNo3 = fNo1 + fNo2;
        }

        System.out.println("Sum of all even nos = "+sumofEvenNo);
    }
}

答案 2 :(得分:0)

我建议使用naming convention,您的代码将更加清晰,易于调试。我也使用static method来获得金额。

  1. 询问总和的计算次数。
  2. 获取索引并计算总和。
  3. 打印。
  4. 类:

    import java.util.*;
    
    public class Abhi {
    
        public static void main(String[] args) {
    
            System.out.println ( "This program will calculate the even sum of given fibonacci series index ( index starts at 1) " );
            System.out.println ( "Please enter how many times do you want to calculate the sum: " );
            Scanner scan = new Scanner(System.in);
            int iterationTimes = scan.nextInt() ;
    
            for (; iterationTimes > 0; iterationTimes-- )
            {
                System.out.println ( "Please, enter the index on fibonacci: " );
                System.out.println ( "Even sum: " + getEvenSum( scan.nextInt() ) );
            }
    
        }
    
        private static long getEvenSum( int index)
        {
            if ( index <= 2 )
            {
                return 0;
            }
    
            long n1=1, n2=1, n3, sum = 0;   
    
            for(int i = 2; i < index; i++) 
            {    
                n3=n1+n2;
                if ( n3 % 2 == 0)
                {
                    sum += n3;
                }
                n1=n2;    
                n2=n3;    
            }  
    
            return sum;
    
        }
    
    
    }
    

    I / O示例:

    This program will calculate the even sum of given fibbonacci series index ( index starts at 1 )
    Please enter how many times do you want to calculate the sum: 
    3
    Please, enter the index on fibbonacci: 
    3
    Even sum: 2
    Please, enter the index on fibbonacci: 
    6
    Even sum: 10
    Please, enter the index on fibbonacci: 
    12
    Even sum: 188
    

    注意: Fibbonaci:1,1,2,3 ......