Baham's Blog

Above all else, guard your heart.

Go语言-二分查找

最近比较懒惰,没有完成读书、以及公开课的既定目标,另外还给自己另外加了一本读书任务《游子吟》。

也没有看手里这本《Go语言云动力》。 所以分享一下,书中go语言下的二分查找代码:


package main

import (
    "fmt"
    "sort"
)

func main() {
    fmt.Println("Pick a number from 0 to 100")
    fmt.Printf("Your number is %d\n",
        sort.Search(100, func(i int) bool {
            fmt.Printf("Is your number <= %d?", "i")
            var s string
            fmt.Scanf("%s\n", &s)
            return s != "" && s[0] == 'y'
        }))
}




The Original Link: http://baham.github.io/11_24_goyu-yan-er-fen-cha-zhao.html
If you want to reprint it, please do under the CC BY-NC-SA 4.0

Comments