题目
题目

COMP9024-Data Structures and Algorithms - 2025 T1

单项选择题

The following algorithmtakes a sorted array A[1..n] of characters and outputs, in reverse order, all 2-letter words νω such that ν≤ω. for all i=n down to 1 do for all j=n down to i do print "A[i]A[j]" end for end forCount the number of primitive operations (evaluating an expression, indexing into an array).

查看解析

查看解析

标准答案
Please login to view
思路分析
We start by restating the problem to ensure we understand what's being counted and when the counts occur. Question: An algorithm processes a sorted array A[1..n] and, for all i from n down to 1, and for all j from n down to i, prints the string formed by A[i]A[j]. The task is to count primitive operations, defined as evaluating an expression or indexing into an array. Option given: a. 2n^2 + 5n + 1 primitive operations. Now, analyze the loop structure and what happens inside the inner loop. - The outer loop runs i from n down to 1, which yields n iterations. ......Login to view full explanation

登录即可查看完整答案

我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。

类似问题

Which of the following statements is true with reference to an algorithm’s runtime?

Consider the same method again: public boolean isprefix(String s1, String s2) { int i = 0; if(s1.length > s2.length) return false; while(i < s1.length) { if(s1[i] != s2[i]) return false; i++; } return true; } Use the active operation approach and determine the exact number of times the active operation is executed in the worst case.  Express your answer in terms of n, the length of the string s1. Hint:  simplify your final answer as much as possible, and do not put spaces in your answer.  Use juxtaposition for the multiplication operator, for example to write "nine times n" write "9n" not "9xn" or "9*n";  to write "four times (n+2)" write "4(n+2)", not "4x(n+2)" or "4*(n+2)".  Do not write your answer in Big-O notation.  Write the exact number of lines executed.

Consider the following method: public boolean isprefix(String s1, String s2) { int i = 0; if(s1.length > s2.length) return false; while(i < s1.length) { if(s1[i] != s2[i]) return false; i++; } return true; } Exactly how many lines of code (statements) are executed by the method isprefix() in the worst case?    Express your answer in terms of n, the length of the string s1.   Hint:  simplify your final answer as much as possible, and do not put spaces in your answer.  Use juxtaposition for the multiplication operator, for example to write "nine times n" write "9n" not "9xn" or "9*n";  to write "four times (n+2)" write "4(n+2)", not "4x(n+2)" or "4*(n+2)".  Do not write your answer in Big-O notation.  Write the exact number of lines executed.

How many times is the diamond (◊) operation performed in the following algorithm? Algorithm mystery(A) Input an array A of n integers Output ???1 value <- A[0] ◊ A[n-1]2 for i <- 1 to n-1 do3 A[i] <- A[i] ◊ 2 4 for i <- 0 up to 316 do5 A[i] <- 3 ◊ A[i] 6  return A[0]

更多留学生实用工具

加入我们,立即解锁 海量真题独家解析,让复习快人一步!