快手b卷 全ac
1.
package main import ( "fmt" "strconv" "strings" ) func main() { var s string fmt.Scanln(&s) ss := strings.Split(s, ".") if len(ss) == 4 { if isIpv4(ss) { fmt.Println("IPv4") return } fmt.Println("Neither") return } ss = strings.Split(s, ":") if len(ss) == 8 { if isIpv6(ss) { fmt.Println("IPv6") return } fmt.Println("Neither") return } fmt.Println("Neither") } func isIpv4(s []string) bool { for i := 0; i < 4; i++ { if s[i] == "" { return false } num, err := strconv.Atoi(s[i]) if err != nil || num < 0 || num > 255{ return false } if len(s[i]) > 1 && s[i][0] == '0' { return false } } return true } func isIpv6(s []string) bool { for i := 0; i < 8; i++ { str := s[i] if str == "" { return false } if str == "00" || str == "000" || str == "0000" { return false } } return true }2.
package main import ( "fmt" ) var m = map[byte]string { '2': "abc", '3': "def", '4': "ghi", '5': "jkl", '6': "mno", '7': "pqrs", '8': "tuv", '9': "wxyz", } func letterCombinations(digits string) []string { if len(digits) == 0 { return []string{} } s := m[digits[0]] res := make([]string, 0) strs := letterCombinations(digits[1:]) strsLen := len(strs) for i := 0; i < len(s); i++ { prefix := s[i:i+1] if strsLen == 0 { res = append(res, prefix) } else { for j := 0; j < strsLen; j++{ res = append(res, prefix + strs[j]) } } } return res } func main() { var s string fmt.Scanln(&s) res := letterCombinations(s) fmt.Print("[") for i := 0; i < len(res); i++ { if i == len(res) - 1 { fmt.Print(res[i]) } else { fmt.Print(res[i]+", ") } } fmt.Println("]") }3.
package main import "fmt" func main() { var n int fmt.Scanln(&n) arr := make([]int, n) sum := 0 for i := 0; i < n; i++ { var tmp int fmt.Scan(&tmp) arr[i] = tmp sum += arr[i] } m := sum / 2 dp := make([]int, m+1) for i := 0; i < n; i++ { for j := m; j > 0; j-- { if j >= arr[i] { if dp[j-arr[i]]+arr[i] > dp[j] { dp[j] = dp[j-arr[i]] + arr[i] } } } } fmt.Println(sum - 2*dp[m]) }4.
package main import ( "fmt" "sort" ) func main() { var n int fmt.Scanln(&n) arr := make([]int, n) for i := 0; i < n; i++ { var tmp int fmt.Scan(&tmp) arr[i] = tmp } if n == 1 { fmt.Println(1) return } if n == 0 { fmt.Println(0) return } sort.Ints(arr) dp := make([][30001]int, n) max := 0 for i := 1; i < n; i++ { for j := 0; j < i; j++ { diff := arr[i] - arr[j] if dp[j][diff] > 0 { dp[i][diff] = getMax(dp[j][diff]+1, dp[i][diff]) } else { dp[i][diff] = 2 } max = getMax(max, dp[i][diff]) } } fmt.Println(max) } func getMax(a, b int) int { if a > b { return a } return b }