learn go recursive

package main

// 参考文章:
//     https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.6.md

import (
    "fmt"
)

func main () {
     fmt.Printf("%d is even: is %t\n", 16, even(16)) // 16 is even is true
    fmt.Printf("%d is odd: is %t\n", 17, odd(17))
    // 17 is odd: is true
    fmt.Printf("%d is odd: is %t\n", 18, odd(18))
    // 18 is odd: is false
}

func even( nr int) bool {
    if nr == 0 {
        return true
    }
    return odd(RevSign(nr) - 1)
}

func odd(nr int) bool {
    if nr == 0 {
        return false
    }
    return even(RevSign(nr) - 1)
}

func RevSign(nr int) int {
    if nr < 0 {
        return -nr
    }
    return nr
}

 

posted on 2015-11-22 17:53  zengjf  阅读(171)  评论(0编辑  收藏  举报

导航