Java:检查 arraylist 是否是斐波那契数列的一部分 [英] Java: check if arraylist is part of fibonacci sequence

查看:36
本文介绍了Java:检查 arraylist 是否是斐波那契数列的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学校的任务是实现一个方法来检查给定的 ArrayList 是否是斐波那契数列的一部分.

My assignment for school is to implement a method that checks if a given ArrayList is part of the Fibonacci sequence.

数组不能为空且必须大于3.

The array must not be empty and must be bigger than 3.

我知道我必须检查数组的一个数字和下一个数字是否是斐波那契数列的一部分,但是我遇到了很多麻烦,因为如果它是数组的任何一部分,您应该接受它顺序,而不仅仅是从一开始.

I understood that I have to check if one number of the array and the next one are part of the Fibonacci sequence, however I have a lot of trouble with it since you're supposed to accept the array if it's any part of the sequence and not just from the start.

例如:0 1 1 2 3 52 3 5 8 13 21 将被接受.

e.g.: 0 1 1 2 3 5 will be accepted as well as 2 3 5 8 13 21.

这是我目前的代码.我知道它非常有缺陷,但我真的不知道如何继续.

This is my code so far. I know it's very flawed but i really have no clue how to move on.

public class ArrayCheck {
 /**
 * Tests if the given array is a part of the Fibonacci sequence.
 *
 * @param arr array to be tested
 * @return true if the elements are part of the fibonacci sequence
 */
public boolean isFibonacci(ArrayList<Integer> arr) {
    //check if array exists
    if(arr.size() == 0)
        return false;

    //check if array is bigger than 3
    if (arr.size() < 3)
        return false;

    //check for the startsequence of 0,1,1
    else if(arr.get(0) == 0 && arr.get(1) == 1 && arr.get(2) == 1)
        return true;

    //check every number in array
    for(int i = 0; i < arr.size(); i++) {
        //check if i >= 2 is fib
        if(i >= 2) {
            int fibn = i;
            int nextfib = i + 1;

            int fibnew = (fibn - 1) + (fibn - 2);
            int fibnext = (nextfib - 1) + (nextfib - 2);

            if (arr.get(i) != fibnew && arr.get(i + 1) != fibnext)
                return false;
        }
        //check if the order is right
        if(arr.get(i) > arr.get(i+1))
            return false;
    }
    return true;
}

非常感谢任何帮助!

推荐答案

好吧,您的代码有一些问题.首先,如果你的数组至少有 3 项,你检查是否只有前三项是斐波那契数列的开始:

Well, you have a few issues with your code. First of all, if you array is at least 3 items, you check if only the first three are the start of the Fibonacci sequence:

//check for the startsequence of 0,1,1
else if(arr.get(0)==0 && arr.get(1)==1 && arr.get(2)==1){
    return true;
}

这很糟糕,因为这意味着不属于序列的 0 1 1 5 将返回 true.

This is bad, as this mean 0 1 1 5 which is not part of the sequence will return true.

您需要做的是将其拆分为两个任务:

What you need to do is split this into two tasks:

  • 找到序列中的第一个相关数字(即,如果数组以 7 开头,则您知道这不是序列的一部分;或者,如果它以 8,你知道你需要从 8 开始检查).
  • 找到开始"后,只需检查数组的其余部分是否遵循斐波那契规则.您需要手动验证前两项.
  • Find the first relevant number in the sequence (i.e. if the array starts with 7, you know this isn't a part of the sequence; alternatively, if it starts with 8, you know you need to start checking from 8 onward).
  • Once you've found the "start", simply check that the rest of the array follows the Fibonacci rule. you'll need to manually verify the first two items.
public boolean isFibonacci(ArrayList<Integer> arr) {

    if (arr.size() < 3){
        return false;
    }

    /** find if the first element is part of the sequence: **/

    int fib1 = 0;
    int fib2 = 1;

    while (fib1 < arr.get(0)) {
        int tmp = fib1 + fib2;
        fib1 = fib2;
        fib2 = tmp;
    }

    if (fib1 != arr.get(0)) {
        // first element is not part of Fibonacci sequence
        return false;
    }

    if (fib2 != arr.get(1)) {
       // the first two elements are not part of the Fibonacci sequence
       return false;
    }

    /*** now simply verify that the rest of the elements uphold the rule:
         each element is the sum of the two previous ones: **/

    for(int i=2; i < arr.size(); i++) {

        // make sure there are no negatives in the array:
        if (arr.get(i) < 0)
           return false;

        if (arr.get(i) != (arr.get(i-1) + arr.get(i-2)))
           return false;

    }

    //everything checks out okay - return true:
    return true;
}

这篇关于Java:检查 arraylist 是否是斐波那契数列的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆