简体   繁体   English

您如何计算Java代码中的操作数,我不确定,但是有人可以验证我的工作吗

[英]How do you count number of operations in java code , i'm not sure but can someone verify my work

Sum <- 0  // 1 Operation 
for i <- 1 to n do // 2n operations
 for j <-1 to n do // 2(n-1) operations
  k <-1 // 1 operation
while k < n do // n-1 operations
k <- k *c // 2 operations 
sum <- sum +1  // 2 operations 

In total the number of operations in the code are : 该代码中的操作总数为:

1+2n+2(n-1)+1+(n-1)+2+2 == 5n+3 total # of operations , 1 + 2n + 2(n-1)+1+(n-1)+ 2 + 2 == 5n + 3个操作总数,

is this how you calculate it , because i understand the concept of each stmt has 3 portions to it ( Comparison, Assignment, Incrementation ) 这是您如何计算的,因为我理解每个stmt的概念都包含3部分(比较,分配,增量)

please feel free to correct me is my observations are incorrect 请随时纠正我,因为我的观察不正确

No, that's probably incorrect. 不,那可能不正确。

First of all: You're thinking too hard. 首先:您想得太辛苦了。 At least for the purposes of Big-O calculations, you can probably treat each assignment as a single operation, no matter whether it's a constant assignment or a calculated value. 至少出于Big-O计算的目的,您可以将每个赋值视为一个单独的操作,而不管它是常量赋值还是计算值。

Second of all: You aren't thinking hard enough. 第二:您没有认真思考。 The fourth line is a single operation, but it's run n * n times, so it should be counted as n^2 , not 1 . 第四行是单个操作,但是它运行了n * n次,因此应计为n^2 ,而不是1 Similarly for other lines in loops. 对于循环中的其他行类似。

暂无
暂无

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

相关问题 我不确定如何在我的Java代码中正确地舍入它 - I'm not sure how to round this properly in my Java code 如何获得按钮actionListener来计算某人单击的次数? - How do I get my button actionListener to count the number of times someone clicks? 正在研究一本旧 JAVA 书的示例问题,我已经发布了我的答案,只是不确定。 有人可以验证答案是否正确 - Was working on a sample question of an old JAVA book, I have posted my answer just not sure about it. Could someone verify if answer is true 如何从我的代码java中的文件中计算每个段落中的单词数? - how can I count number of words from each paragraph from the file in my code java? 如何计算扫雷代码中的炸弹数量? - How do I count the number of bombs in my Minesweeper code? 如何计算此Java代码中用户输入的数量? - How do I Count the number of user inputs in this java code? 有人可以解释Java嵌套循环如何与数字模式一起使用吗? - Can someone explain how Java nested loops work with number patterns? 关于如何计算验证“是否”条件的元素数量有任何想法吗? - Any idea on how can I count the number of elements that verify an “if” condition? 无法让我的复制构造函数在我的简单代码中工作(我是 java 新手) - Can't get my copy constructor to work in my simple code (I'm new to java) 如何创建代码来计算数组列表中的字符数? (爪哇) - How do you create a code to count the number of characters in an array list? (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM