题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
package main import ( "fmt" "strconv" "strings" "sort" ) func getMatchIdx(n int, I []int) []int { var idxs []int ns := strconv.Itoa(n) for i, number := range I { rs := strconv.Itoa(number) if strings.Contains(rs, ns) { idxs = append(idxs, i) } } return idxs } func calculate(I []int, R []int) { var ans []int // 先对 R 进行排序和去重 sort.Ints(R) set := make(map[int]struct{}) var idx int for _, r := range R { if _, ok := set[r]; !ok { set[r] = struct{}{} R[idx] = r idx++ } } R = R[:idx] // fmt.Printf("sorted R: %+v\n", R) // 对 R 中的每一个元素进行统计 for _, r := range R { idxs := getMatchIdx(r, I) if len(idxs) == 0 { continue } ans = append(ans, r) ans = append(ans, len(idxs)) for _, idx := range idxs { ans = append(ans, idx) ans = append(ans, I[idx]) } } var s string s += fmt.Sprintf("%d ", len(ans)) for _, num := range ans { s += fmt.Sprintf("%d ", num) } fmt.Println(s[:len(s)-1]) } func main() { var m int fmt.Scan(&m) var I []int for i:=0; i<m; i++ { var num int fmt.Scan(&num) I = append(I, num) } var n int fmt.Scan(&n) var R []int for i:=0; i<n; i++ { var num int fmt.Scan(&num) R = append(R, num) } calculate(I, R) }
// 本题输入为两行数字,中间用空格分开,所以采用:fmt.Scan(&num)