求三个数中较大两个之和

构造过程抽象
2015-01-03 16:10

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


两两比较,要考虑较大数可能是同一个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    (define (get-the-max a b) 
      (if (> a b) 
          a
          b))
    
    (define (sum-of-the-max a b c)
      (if (= (get-the-max a b) (get-the-max b c)) 
          (+ (get-the-max a b) (get-the-max a c))
          (+ (get-the-max a b) (get-the-max b c)))
    )
    
    (sum-of-the-max 1 2 3)
    (sum-of-the-max 1 2 1)
    (sum-of-the-max 1 1 1)
    (sum-of-the-max -1 0 1)
    (sum-of-the-max -1 0 0)
    (sum-of-the-max -2 -1 0)

输出

1
2
3
4
5
6
7
    5
    3
    2
    1
    0
    -1
    >

执行 97 步

另解

较大的两个数之和也较大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    (define (get-the-max a b) 
      (if (> a b) 
          a
          b))
    
    (define (get-the-max-in-3 a b c)
      (get-the-max (get-the-max a b) c))
    
    (define (sum-of-the-max a b c)
      (get-the-max-in-3 (+ a b)(+ b c)(+ c a)))
    
    (sum-of-the-max 1 2 3)
    (sum-of-the-max 1 2 1)
    (sum-of-the-max 1 1 1)
    (sum-of-the-max -1 0 1)
    (sum-of-the-max -1 0 0)
    (sum-of-the-max -2 -1 0)

输出

1
2
3
4
5
6
7
    5
    3
    2
    1
    0
    -1
    >

执行 67 步

加载 Disqus 评论

© 版权所有 悟理 2013-2019
Powered By Jekyll & Semantic UI

求三个数中较大两个之和

构造过程抽象
2015-01-03 16:10

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


两两比较,要考虑较大数可能是同一个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    (define (get-the-max a b) 
      (if (> a b) 
          a
          b))
    
    (define (sum-of-the-max a b c)
      (if (= (get-the-max a b) (get-the-max b c)) 
          (+ (get-the-max a b) (get-the-max a c))
          (+ (get-the-max a b) (get-the-max b c)))
    )
    
    (sum-of-the-max 1 2 3)
    (sum-of-the-max 1 2 1)
    (sum-of-the-max 1 1 1)
    (sum-of-the-max -1 0 1)
    (sum-of-the-max -1 0 0)
    (sum-of-the-max -2 -1 0)

输出

1
2
3
4
5
6
7
    5
    3
    2
    1
    0
    -1
    >

执行 97 步

另解

较大的两个数之和也较大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    (define (get-the-max a b) 
      (if (> a b) 
          a
          b))
    
    (define (get-the-max-in-3 a b c)
      (get-the-max (get-the-max a b) c))
    
    (define (sum-of-the-max a b c)
      (get-the-max-in-3 (+ a b)(+ b c)(+ c a)))
    
    (sum-of-the-max 1 2 3)
    (sum-of-the-max 1 2 1)
    (sum-of-the-max 1 1 1)
    (sum-of-the-max -1 0 1)
    (sum-of-the-max -1 0 0)
    (sum-of-the-max -2 -1 0)

输出

1
2
3
4
5
6
7
    5
    3
    2
    1
    0
    -1
    >

执行 67 步

加载 Disqus 评论

© 版权所有 悟理 2013-2019
Powered By Jekyll & Semantic UI

求三个数中较大两个之和

构造过程抽象
2015-01-03 16:10

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


两两比较,要考虑较大数可能是同一个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    (define (get-the-max a b) 
      (if (> a b) 
          a
          b))
    
    (define (sum-of-the-max a b c)
      (if (= (get-the-max a b) (get-the-max b c)) 
          (+ (get-the-max a b) (get-the-max a c))
          (+ (get-the-max a b) (get-the-max b c)))
    )
    
    (sum-of-the-max 1 2 3)
    (sum-of-the-max 1 2 1)
    (sum-of-the-max 1 1 1)
    (sum-of-the-max -1 0 1)
    (sum-of-the-max -1 0 0)
    (sum-of-the-max -2 -1 0)

输出

1
2
3
4
5
6
7
    5
    3
    2
    1
    0
    -1
    >

执行 97 步

另解

较大的两个数之和也较大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    (define (get-the-max a b) 
      (if (> a b) 
          a
          b))
    
    (define (get-the-max-in-3 a b c)
      (get-the-max (get-the-max a b) c))
    
    (define (sum-of-the-max a b c)
      (get-the-max-in-3 (+ a b)(+ b c)(+ c a)))
    
    (sum-of-the-max 1 2 3)
    (sum-of-the-max 1 2 1)
    (sum-of-the-max 1 1 1)
    (sum-of-the-max -1 0 1)
    (sum-of-the-max -1 0 0)
    (sum-of-the-max -2 -1 0)

输出

1
2
3
4
5
6
7
    5
    3
    2
    1
    0
    -1
    >

执行 67 步

加载 Disqus 评论

© 版权所有 悟理 2013-2019
Powered By Jekyll & Semantic UI