找出斐波那契数列中小于或等于该数字的所有数字的总和

find the sum of all the numbers in the Fibonacci series that are smaller or equal to that number

斐波那契数列是以下整数序列中的数字。

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 . . .

给定 Fn 系列中的特定数字,您的程序需要找到斐波那契系列中小于或等于该数字的所有数字的总和。

输入格式 您的程序将在 STDIN 上的斐波那契数列中提供一个数字。

约束条件

0<<Fn<100000

示例输入

8

示例输出

The output for above input (8) should be 0 + 1 + 1 + 2 + 3 + 5 + 8
20

为了解决上面的问题我写了这样的代码

import java.util.*;
import java.lang.*;
import java.io.*;

class MyTest{
    public static Integer solveProblem(int n) {
        if (n>=0 || n<100000) {
        int fibo[] = new int[n+1];
        fibo[0] = 0; fibo[1] = 1;
        int sum = fibo[0] + fibo[1];
        for (int i=2; i<=n; i++) {
            fibo[i] = fibo[i-1]+fibo[i-2];
            if(n >= fibo[i]) {
                sum += fibo[i];
            } else {
                break;
            }
        }
        return sum;
        } else {
            return 0;    
        }
    }

    public static void main (String[] args) throws java.lang.Exception {
        Scanner in = new Scanner(System.in);
        int sum = MyTest.solveProblem(in.nextInt());
        System.out.println("This is the output:"+sum);
    }
}

我运行这个程序它在上面提到的示例测试用例中工作得很好但是当我在在线测试测验中提交这个程序然后它运行用他们的测试用例并且它会失败。

是我的程序有问题还是我没有正确理解问题。请帮我找出这个问题的确切答案。

您应该从扫描器中获取值

Scanner in = new Scanner(System.in);
int sum = MyTest.solveProblem(8);

通过使用 Scanner.nextInt()

Scanner in = new Scanner(System.in);
int sum = MyTest.solveProblem(in.nextInt());

如果您输入一个值 0,它被您的条件所接受,但您创建了一个大小为 1 的数组,然后尝试访问第二个值 -> ArrayIndexOutOfBoundsException

int fibo[] = new int[n+1];
fibo[0] = 0; 
fibo[1] = 1; //HERE, exception as there is not `fibo[1]`

更新条件为

if ( n > 0 && n < 100000) //note the && to correct your logic error too

注意:我认为在这里使用数组不是一个好主意,因为您使用的是一个不必要的大数组(未完全使用),使用两个变量(last,current)会更简单.

您的代码似乎在 n=0 和 n=1 时失败。

  • 对于 n=1 你 return 1 但应该 return 2(因为有两个元素的值为 1,你应该将它们都加到总和中)。
  • 对于 n=0,您将引发异常。

您可以通过为这些情况添加特殊检查来解决此问题:

public static Integer solveProblem(int n) {
    if (n==0) 
        return 0;
    else if (n==1) 
        return 2;
    else if (n>=0 || n<100000) {
        int fibo[] = new int[n+1];
        fibo[0] = 0; fibo[1] = 1;
        int sum = fibo[0] + fibo[1];
        for (int i=2; i<=n; i++) {
            fibo[i] = fibo[i-1]+fibo[i-2];
            if(n >= fibo[i]) {
                sum += fibo[i];
            } else {
                break;
            }
        }
        return sum;
    } else {
        return 0;    
    }
}
import java.util.*;
import java.lang.*;
import java.io.*;

class CutShort
{
    public static long solveProblem(int input) {        

        long sum = 0;        

        long finalstate = 1;
        long currentstate = 0;

        while(finalstate<=input) {

            sum = sum+finalstate;

            long add = currentstate + finalstate;

            currentstate = finalstate;

            finalstate = add;
        }
        return sum;

    }


public static void main (String[] args) throws java.lang.Exception

    {

        Scanner in = new Scanner(System.in);

        /*Parse input here*/

        System.out.println("Enter you Input");

        int input = in.nextInt();

        long output = 0;

        if(input>0 && input<100000)

        output = solveProblem(input);

        System.out.println("This is the output" + output);

    }

}