java - 为什么这个算法会为除 0 和 1 之外的所有种子打印斐波那契/三波那契序列?

标签 java fibonacci

我正在尝试使用从用户输入获得的种子值打印有限数量的 tribonacci 序列(其中任何项的值等于前面三个项的总和,即 [2,3,5,10,18,33...])。使用除 0 和 1 之外的任何其他正值,代码始终有效,如下所示:

2
3
5
Printing...
[2, 3, 5, 10, 18, 33, 61, 112, 206, 379, 697, 1282, 2358, 4337, 7977, 14672, 26986, 49635, 91293, 167914]
Process finished with exit code 0

但是以 0、1 和 1 作为种子,我得到:

0
1
1
Printing...
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Process finished with exit code 0

这是代码(我知道过于明确的注释,这是有原因的):

import java.util.Arrays;
import java.util.Scanner;

public class Tribonacci {

public static void main(String[] args) throws InterruptedException {
    System.out.printf("Hello!\n");
    Thread.sleep(2000);
    System.out.printf("The purpose of this program is to display the first twenty terms of either a Fibonacci sequence or a Tribonacci sequence\n");
    System.out.printf("To start, type \"F\" for Fibonacci or \"T\" for Tribonacci:\n");//Give prompt options
    Scanner seqeuence = new Scanner(System.in);
    String typeseq = seqeuence.next();//get user input, based on ^ prompt ^
    printSequence(typeseq);
}

private static void printSequence(String typeOfSequence) throws InterruptedException {


    //checks for which kind of sequence they want to print based on above prompt
    switch (typeOfSequence) {
        case "T"://if user types "T"

            System.out.printf("You have chosen to print the first twenty terms of the Tribonacci sequence\n");
            Thread.sleep(2000);
            System.out.printf("We will need the seed terms for this to work, so input THREE non-negative integers, in numerical order:\n");
            Scanner getTrib = new Scanner(System.in);
            int[] tSeeds = new int[3];//scans for three seeds from the user, stores in an array of size 3

            //put each seed term into an array, in order to perform arithmetic on the elements later
            for (int i = 0; i < tSeeds.length; i++) {
                tSeeds[i] = getTrib.nextInt();
            }

            int[] newTSeq = new int[20];//final array to be printed
            newTSeq[0] = tSeeds[0];
            newTSeq[1] = tSeeds[1];
            newTSeq[2] = tSeeds[2];
            int incrementT = 0;//used to traverse the array and move which seeds should be summed
            for (int itemT : newTSeq) {

                if (itemT == tSeeds[0] || itemT == tSeeds[1] || itemT == tSeeds[2]) {
                    continue;

                } else {
                    newTSeq[3 + incrementT] = newTSeq[incrementT] + newTSeq[incrementT + 1] + newTSeq[incrementT + 2];
                    ++ incrementT;
                }
            }

            System.out.printf("Printing...\n");
            Thread.sleep(2000);
            System.out.print(Arrays.toString(newTSeq));//terms are converted into strings to be printed

            break;

        case "F"://if user types "F"

            System.out.printf("You have chosen to print the first twenty terms of the Fibonacci sequence\n");
            Thread.sleep(2000);
            System.out.printf("We will need the seed terms for this to work, so input TWO non-negative integers, in numerical order:\n");
            Scanner getFib = new Scanner(System.in);
            int[] fSeeds = new int[2];//scan user input of TWO integers into an array that holds TWO integers

            //put each seed term into array for later computation, like above
            for (int i = 0; i < fSeeds.length; i++) {
                fSeeds[i] = getFib.nextInt();
            }

            int[] newFSeq = new int[20];//final array to be printed
            newFSeq[0] = fSeeds[0];
            newFSeq[1] = fSeeds[1];
            int incrementF = 0;
            for (int itemF : newFSeq) {
                if (itemF == newFSeq[0] || itemF == newFSeq[1]) { //if the iterator is on one of the seeds, don't sum
                    continue;
                } else {
                    newFSeq[2 + incrementF] = newFSeq[incrementF] + newFSeq[incrementF + 1];//sums the two preceding terms
                    incrementF++;
                }
            }

            System.out.printf("Printing...\n");
            Thread.sleep(2000);
            System.out.print(Arrays.toString(newFSeq));//convert all array values to strings, to be printed

            break;

        default://if neither case is satisfied, print this message
            System.out.printf("Restart the program. Next time, input the SUGGESTED prompt items \"F\" for Fibonacci or \"T\" for Tribonacci");
    }
}
}

最佳答案

嗯,你的情况很奇怪

if (itemT == tSeeds[0] || itemT == tSeeds[1] || itemT == tSeeds[2]) {
    continue;
}

这意味着,如果 tSeeds[0]tSeeds[1]tSeeds[2] 中的任何一个为 0,则不会向 newTSeq 数组分配任何内容(因为 itemT 对于 newTSeq 的所有元素默认为 0,并且在 0,1,1 输入的情况下,您只需更改在循环之前将 newTSeq[1]newTSeq[2] 更改为 1)。

我不确定这个条件应该做什么。您也许可以将其删除。

关于java - 为什么这个算法会为除 0 和 1 之外的所有种子打印斐波那契/三波那契序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45006170/

相关文章:

java - 任何人都可以解释以下代码的工作......?

java - Java 中的客户端-服务器应用程序 - 单例问题

c++ - 编写一个程序,计算并输出前 N 个奇数斐波那契数,以逗号和空格分隔。 N 从标准输入输入

c++ - 斐波那契输出的 while 循环条件

algorithm - 找出非常大的 'n' 的第 n 个斐波那契数

java - 问题 : I want to delete directory from my maven project when i am running mvn eclipse:clean

java - 如何在两个容器中运行 Jade 代理(从两个控制台)

java - 在 JUnit5 (Eclipse) 中创建 TestSuite

c - 使用黄金比例(黄金数)的斐波那契递归

c++ - 编译处理大量数字的时间斐波那契