我需要帮助了解运行时间和最坏的情况

时间:2021-10-01 02:04:42

My professor was trying to get me to understand running time and worst case but I'm still confused. He say look at the for loop and see how many times it iterate and I guess for this code i have below it iterate n number of times. I just not too sure though. It for the Fibonacci sequence.

我的教授试图让我了解运行时间和最坏的情况,但我仍然感到困惑。他说看看for循环,看看它迭代了多少次,我想这个代码我在它下面迭代n次。我不太确定。它用于斐波纳契数列。

for (int i = 0; i < t; i++) {
        j[i] = q;
        int A = q;
        q = u;
        u = A + q;
    }

    for (int m = 0; m < b; m++) {
        if (j[m] <= b) {
            System.out.print(j[m]);
        }
    }

1 个解决方案

#1


3  

Yes. The complexity of the code is O(t) as the loop is running t times. Inside loop you are calculating the next fibonacci number and storing them into an array j[].

是。代码的复杂性是O(t),因为循环运行t次。在内部循环中,您正在计算下一个斐波那契数并将它们存储到数组j []中。

Next you are printing the content of the array, which is also simple iteration over the array.

接下来,您将打印数组的内容,这也是对数组的简单迭代。

I would suggest you to always use relevant and meaningful variable name like n instead of t and b while looping. Also you should name the array properly like int fibonacci[] instead of int j[]. This type of code is always self explanatory.

我建议你在循环时总是使用相关且有意义的变量名,比如n而不是t和b。你也应该像int fibonacci []而不是int j []一样正确地命名数组。这种类型的代码总是不言自明。

#1


3  

Yes. The complexity of the code is O(t) as the loop is running t times. Inside loop you are calculating the next fibonacci number and storing them into an array j[].

是。代码的复杂性是O(t),因为循环运行t次。在内部循环中,您正在计算下一个斐波那契数并将它们存储到数组j []中。

Next you are printing the content of the array, which is also simple iteration over the array.

接下来,您将打印数组的内容,这也是对数组的简单迭代。

I would suggest you to always use relevant and meaningful variable name like n instead of t and b while looping. Also you should name the array properly like int fibonacci[] instead of int j[]. This type of code is always self explanatory.

我建议你在循环时总是使用相关且有意义的变量名,比如n而不是t和b。你也应该像int fibonacci []而不是int j []一样正确地命名数组。这种类型的代码总是不言自明。