题解 | #合法IP#
合法IP
http://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
package main
import (
"fmt"
"io"
"strings"
"strconv"
)
func main() {
for {
var (
s string
)
_, err := fmt.Scan(&s)
if err == io.EOF {
return
}
ips := strings.Split(s, ".")
if len(ips) != 4 {
fmt.Println("NO")
return
}
var flag bool
for i := range ips {
num, _ := strconv.Atoi(ips[i])
if num < 0 || num > 255 {
flag = true
break
}
}
if !flag {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
}