考虑一个身体为:
的函数sum = 0;
for (i = 1; i <= f(n); i++)
sum += i;
其中f(n)是函数调用。在这个函数的运行时间上给出一个简单而紧凑的大上限,作为n的函数假设:
a) The running time of f(n) is O(n), and the value of f(n) is n!
b) The running time of f(n) is O(n), and the value of f(n) is n
c) The running time of f(n) is O(n²), and the value of f(n) is n
d) The running time of f(n) is O(1), and the value of f(n) is 0
我只是想确保自己走在正确的轨道上。我的回答:
a) O(n²)
b) O(n²)
c) O(n³)
d) O(1)
答案 0 :(得分:2)
第一个不正确:f(n)的值为n!,即factorial。它变得非常快。答案应该是O(n×n!),因为你正在调用一个运行时间为O(n)的函数总共n!倍。我不确定这是否可以简化。
其他人看起来很好。