package main
import "fmt"
func main() {
arrStr := [4]string{"小王", "老王", "王八", "王王"}
var name = ""
fmt.Scanln(&name )
//for i := 0;i < len(arrStr);i++ {
// if name == arrStr[i] {
// fmt.Println(name,"已找到","下标为",i)
// break
// } else if i == (len(arrStr)) {
// fmt.Println(name,"没有找到")
// }
//}
index := -1
for i := 0;i < len(arrStr);i++ {
if name == arrStr[i] {
index = i
break
}
}
if index != -1 {
fmt.Println("找到了")
}
}
package main
import "fmt"
func selectArr(arr *[6]int, leftIndex int, rightIndex int, findVal int ) {
//找到中间下标
middle := (leftIndex + rightIndex) / 2
//当左边的下标大于右边下标
if leftIndex > rightIndex {
fmt.Println("没有找到这个数")
return
}
//当中间下标的值大于传入的值往左边查找
if (*arr)[middle] > findVal {
selectArr(arr,leftIndex,middle - 1,findVal)
//当中间下标的值小于传入的值往右边查找
} else if (*arr)[middle] < findVal {
selectArr(arr,middle + 1,rightIndex,findVal)
} else {
fmt.Println("已找到该数,下标为:",middle)
}
}
func main() {
var arr = [6]int{40,55,66,85,99,400}
selectArr(&arr,0,len(arr) - 1,66)
}