gpt4 book ai didi

algorithm - 以下程序的运行时间是多少?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:59:31 25 4
gpt4 key购买 nike

我书中的练习要求我计算以下 for 循环的运行时间:

for (int i = 0; i < n; ++i)
++k;

这立刻让我想起求和符号。所以我写下了适当的语法:

enter image description here

这是正确的吗?如果不是,为什么不 - 我怎样才能正确计算它?

最佳答案

Is this correct?

没有。它不是。

If not, why not - and how can I calculate it correctly?

k 在每次迭代中递增 1,即当循环终止时将 n 添加到它。所以,k = k + 1 不等于 k = k + k

代码的运行时间是 n 的顺序,因为循环运行了 n 次并且 ++k 是在常数时间内完成的循环。

关于algorithm - 以下程序的运行时间是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25704667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com