You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package cap3
|
|
|
|
|
|
|
|
type Candidate int // 候选人
|
|
|
|
type Vote int // 票数
|
|
|
|
|
|
|
|
// Ex11 投票统计,有调整,这里不使用数组存储结果,使用hashmap表存结果
|
|
|
|
func Ex11(votes []Candidate, numOfCandidate int) map[Candidate]Vote {
|
|
|
|
result := map[Candidate]Vote{}
|
|
|
|
for _, candidate := range votes {
|
|
|
|
// 无效的投票
|
|
|
|
if candidate <= 0 || int(candidate) > numOfCandidate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := result[candidate]; !exists {
|
|
|
|
result[candidate] = 1
|
|
|
|
} else {
|
|
|
|
result[candidate]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|