Lecture 5

mykingyIP属地: 北京
字数 138

A. recursive algorithm

1.

-Reduce a problem to a simpler (or smaller) version of the same problem, plus some simple computations

• Recursive step

-Keep reducing until reach a simple case that can be solved directly

• Base case

ex: a * b = a; if b = 1 (Base case)

      a * b = a + a * (b-1); otherwise (Recursive case)

2. Euclidean algorithm

The greatest common divisor of two positive integers is the largest integer that divides each of them without remainder.

suppose that a and b are two positive integers:

If b = 0, then the answer is a

Otherwise, gcd(a, b) is the same as gcd(b, a % b)

3.  other instances for recursive: Fibonacci numbers/ tower of Hanoi

4. recursion on strings: #palindrome

#认真思考 base case: L5P8/P9

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
0人点赞
总资产1共写了1922字获得0个赞共1个粉丝

推荐阅读更多精彩内容