优质操作的数量 [英] Number of primetive operations
问题描述
我是算法设计和分析的新手。我有一个嵌套循环和if语句。我无法确定在if语句中完成的基本操作。声明是(b = b; i< n; i ++)
for(j = 0; j< n; j ++) )
if(i!= j和A [i] == A [j])
duplicate = true
break;
如果(重复)
休息;
我尝试过:
i确定if语句中的操作次数如下:
访问数组元素2次
比较我和
比较A [i]和A [j]
比较AND运算符
这一切都在进行中N次。我正确地猜测if语句中的原始操作数量吗?如果没有,那么请帮我纠正这个。在此先感谢
I am new to Design and Analysis of Algorithms. I have a nested loop and and if statement.I am unable to determine the primitive operations being done in if statement. The statement is
for (i=0;i<n;i++)
for(j=0;j<n;j++)
if(i!=j and A[i]==A[j])
duplicate=true
break;
if(duplicate)
break;
What I have tried:
i am determining the No of operations in if statement as follows:
Accessing array element 2 Times
comparing i and
Comparing A[i] and A[j]
Comparing AND Operator
all this is being done N times. Am i right in guessing the number of primitive operations in if statement? if not then please help me correct this. Thanks in advance
推荐答案
如果我们添加更多的空格和括号,这是相当简单的:
This is fairly straightforward if we add some more spaces and parentheses:
if ( (i != j) and (A[i] == A[j]) )
v v v
| | +-----> equality operator comparing two array objects
| +---------------> AND logical operator testing if both expressions are true
+---------------------> inequality operator comparing i and j
- 首先测试我是否不等于j。
- 如果这是真的那么AND运算符意味着我们必须测试下一个表达式
- 测试数组元素A [i ]等于A [j]
- 如果两者都为真则整个表达式为真
- first test if i is not equal to j.
- if that is true then the AND operator means we must test the next expression
- test if array element A[i] is equal to A[j]
- If both are true then the whole expression is true
这篇关于优质操作的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!