题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
package main import ( "fmt" ) func main() { var n int fmt.Scan(&n) m := make(map[string]int) var candidates []string for i:=0; i<n; i++ { var candidate string fmt.Scan(&candidate) candidates = append(candidates, candidate) m[candidate] = 0 } var invalidVoter int var voteCnt int fmt.Scan(&voteCnt) for i:=0; i<voteCnt; i++ { var voter string fmt.Scan(&voter) if _, ok := m[voter]; !ok { invalidVoter++ } else { m[voter]++ } } for _, candidate := range candidates { fmt.Printf("%s : %d\n", candidate, m[candidate]) } fmt.Printf("%s : %d\n", "Invalid", invalidVoter) }
// 本题输入为每行数字和字符串,所以采用:fmt.Scan(&n)