简体   繁体   English

我想从斐波那契数列中减去最大可能的数字

[英]I want to subtract the largest possible number from the Fibonacci sequence by decreasing it in

I want to get n from user and subtract the largest possible number from the Fibonacci sequence by decreasing it in sequence and continue this process until it reaches zero.我想从用户那里得到n并通过按顺序减少它从斐波那契数列中减去最大可能的数字并继续这个过程直到它达到零。 Our Fibonacci sequence starts from 1. The number 88 can be The form 1 + 3 + 8 + 21 + 55 wrote that these numbers are the sentences 1, 3, 5, 7 and 9 of the Fibonacci sequence, respectively.我们的斐波那契数列从 1 开始。数字 88 可以是 1 + 3 + 8 + 21 + 55 的形式写到这些数字分别是斐波那契数列的句子 1、3、5、7 和 9。

Input: 88输入: 88

Output: 9 7 5 3 1输出: 9 7 5 3 1

My code:我的代码:

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

        int n = input.nextInt();
        int[] fibo = new int[1000];
        int[] arr = new int [100];
        int d = 0;
        int b = 0;
        fibo[1] = 1;
        fibo[2] = 2;

        for (int i = 3, j = 1; i <= 20; i++, j++)
            fibo[i] = fibo[i - 1] + fibo[i - 2];

        b = n;

        for (int i = 1, j = 1; i <= 20 && b >= 0; i++, j++) {
            if (b == fibo[i] || b + 1 == fibo[i] || b - 1 == fibo[i]) {
                d = i;
                b -= fibo[d - 1];
                System.out.println("B= " + b);
                arr[j] = d - 1;
                System.out.println("arr= " + arr[j]);
            }
        }
    }
}

The first problem is that, every time you subtract something from your number, the next number you will subtract will have to be smaller - because your number itself is smaller now - whereas in your code, the numbers just keep getting bigger.第一个问题是,每次从数字中减去某个值时,您将减去的下一个数字必须更小 - 因为您的数字本身现在更小 - 而在您的代码中,数字只会越来越大。

For instance, after you try the 9th fibonacci number 55, you are trying 89, whereas you should be trying 34.例如,在您尝试第 9 个斐波那契数 55 之后,您正在尝试 89,而您应该尝试 34。

This can be solved by iterating backwards through the fibonacci numbers, as in starting from the biggest number at the end of the array, so that as the loop iterates, the numbers get smaller.这可以通过向后迭代斐波那契数来解决,就像从数组末尾的最大数字开始一样,这样随着循环的迭代,数字变小。

The second problem is your check in the if statement, which is only satisfied if there happens to be a fibonacci number very close to the number you are at.第二个问题是你在 if 语句中的检查,只有当恰好有一个与你所在的数字非常接近的斐波纳契数时才会满足。

Instead, if you want to select the largest fibonacci number that is smaller than your number, you can just check for the 'smaller than your number' part, since 'the largest fibonacci' part is already taken care of by how the for loop works.相反,如果您想选择小于您的数字的最大斐波那契数,您可以只检查“小于您的数字”部分,因为“最大斐波纳契”部分已经由 for 循环的工作方式处理.

Here is the working code.这是工作代码。 You will also notice a lot fewer variables.您还会注意到变数少了很多。 Also I hard-coded the input number for testing, you'll have to add the scanner stuff back in. Sorry.此外,我对输入编号进行了硬编码以进行测试,您必须重新添加扫描仪的内容。抱歉。

public class Main {
    public static int len = 20; // the program considers this many fibonacci numbers
    
    public static void main(String args[])
    {
        int n = 88;
        int[] fibo = new int[len];
        
        for(int i=0; i<len; i++)
        {
            if (i < 2) fibo[i] = i+1; // for 1 and 2
            else fibo[i] = fibo[i-1] + fibo[i-2]; // for 3 and 5 and ...
        }
        
        for(int i=len-1; i>=0; i--)
        {
            if (fibo[i] <= n)
            {
                System.out.format("%d: %d - %d = %d\n",
                    i+1, // to correct for 0-indexing
                    n, fibo[i], n-fibo[i]);
                n -= fibo[i];
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM