中文翻译书籍:

SICP之1.3中文翻译题目错误-LMLPHP

翻译错误,题目正确为:

请定义一个过程,它以三个数为参数,返回其中较大的两个数平方之和。

网上的参考答案为:

 (define (square x) (* x x))

 (define (sumsquares x y) (+ (square x) (square y)))

 (define (sqsumlargest a b c)
(cond
((and (>= a c) (>= b c)) (sumsquares a b))
((and (>= b a) (>= c a)) (sumsquares b c))
((and (>= a b) (>= c b)) (sumsquares a c))))

我个人的答案是:

(define (sum-max-three x y z)
(define (bigger a b) (if (< a b)
b
a))
(define (smaller a b)
(if (= (bigger a b) b)
a
b))
(define (square x)
(* x x))
(+ (square (bigger x y)) (square (bigger (smaller x y) z) )))

思路是:两个数比较,取两个数中最大一个平方 加上 前面两个数中较小的如第三个数比较取较大的平方。

列如 1 2 3 max(1,2)-> 2 max(1,3)-> 3 2平方 + 3平方。

05-18 18:55