算法设计与程序分析习题精选含答案(第二章)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

作业一

2.1.1For each of the following algorithms, indicate(i)a natural size metric for its inputs,(i) its basic operation, and(ii) whether the basic operation count can be different for inputs of the same size:

a. computing the sum of n numbers

b. computing n!

c. finding the largest element in a list of n numbers

d. Euclid's algorithm

e. sieve of Eratosthenes

f. pen-and-pencil algorithm for multiplying two n-digit decimal integers

2.1.4 a. Glove selection There are 22 gloves in a drawer:5 pairs of red gloves,4 pairs of yellow, and 2 pairs of green. You select the gloves in the dark and can check them only after a selection has been made. What is the smallest number of gloves you need to select to have at least one matching pair in the best case? In the worst case?

b. Missing socks Imagine that after washing 5 distinct pairs of socks, you discover that two socks are missing. Of course, you would like to have the largest number of complete pairs remaining. Thus, you are left with 4 complete pairs in the best-case scenario and with 3 complete pairs in the worst case. Assuming that the probability of disappearance for each of the 10socks is the same, find the probability of the best-case scenario; the probability of the worst-case scenario; the number of pairs you should expect in the average case.

答:

a.最好的情况是2只,最差的情况是12只

b.在袜子丢失中,丢失的总情况数为:种,

2.1.9 For each of the following pairs of functions, indicate whether the first function of each of the following pairs has a lower, same, or higher order of growth(to within a constant multiple) than the second function.

答:

2.2.3 For each of the following functions, indicate the class(g(n)) the function belongs to.(Use the simplest g(n) possible in your answers.) Prove your assertions.

答:

2.2.4 a. Table 2.1 contains values of several functions that often arise in the analysis of algorithms. These values certainly suggest that the functions

are listed in increasing order of their order of growth. Do these values prove this fact with mathematical certainty?

b. Prove that the functions are indeed listed in increasing order of their order of growth.

答:

a.随着n的增大这些函数都会趋于正无穷,没有一个确定的函数值能证明他们的顺序。

b.

2.2.5List the following functions according to their order of growth from the lowest to the highest:

答:

由前至后分别属于的类型为:阶乘,对数,指数,幂函数,对数的平方,幂函数,指数函数,具体的优先级为:

2.3.4 Consider the following algorithm.

ALGORITHM Mystery(n)

//Input: A nonnegative integer n

S←0

for i←1to n do

S←S+ i∗i

return S

a. What does this algorithm compute?

b. What is its basic operation?

c. How many times is the basic operation executed?

d. What is the efficiency class of this algorithm?

e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done.

答:算法求得是n项的平方和,基本操作是乘法,这个操作执行了n次,效率类型为,

相关文档
最新文档