华为机考:计算打靶分数最高问题

题目的意思是输入n个打把成绩,是不同的人打靶情况,以id区分不同的人,如果某个人打的靶子次数小于三个,则成绩无效。在每个人的打靶成绩中取最高的三个成绩求和作为总成绩,最后将每个人的总成绩从高到低排列输出。如果出现同分,则比较id号的大小,id较大者排位靠前。
比如说输入

10
3,3,4,5,5,3,4,5,5,4
84,58,95,32,74,95,82,43,45,25
package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
    "strconv"
    "strings"
)

type People struct {
    Id int
    Score int
}

func main() {
    var n int
    var s1, s2 []string
    var ids,score []int
    input := bufio.NewScanner(os.Stdin)
    input.Scan()
    n, _ = strconv.Atoi(input.Text())
    input.Scan()
    s1 = strings.Split(input.Text(),",")
    for i :=0; i< n;i++ {
        a, _ := strconv.Atoi(s1[i])
        ids = append(ids,a)
    }
    input.Scan()
    s2 = strings.Split(input.Text(),",")
    for i :=0; i< n;i++ {
        a, _ := strconv.Atoi(s2[i])
        score = append(score,a)
    }
    m := map[int] []int{}
    for i :=0; i< len(ids);i++ {
        m[ids[i]] = append(m[ids[i]],score[i])
    }
    res := deal(m)
    var ans string
    for i:=len(res)-1;i>=0;i--{
        if i != 0 {
            ans = fmt.Sprintf("%s%d%s",ans,res[i].Id,",")
        } else {
            ans = fmt.Sprintf("%s%d",ans,res[i].Id)
        }
    }
    fmt.Println(ans)
}

func deal(m map[int] []int) []*People{
    maxScore := make(map[int]int)

    for k,v := range m {
        if len(v) < 3 {
            continue
        }
        sort.Ints(m[k])
        l := len(v)
        max := m[k][l-1]+m[k][l-2]+m[k][l-3]
        maxScore[k] = max

    }
    var peoples []*People
    for k,v := range maxScore {
        peoples = append(peoples,&People{
            Id: k,
            Score: v,
        })
    }
    sort.Slice(peoples,func(i int,j int) bool {
        return peoples[i].Score < peoples[j].Score
    })
    for i :=0; i <len(peoples); i++ {
        if i >0 && peoples[i].Score == peoples[i-1].Score && peoples[i].Id<peoples[i-1].Id {
            temp1 := peoples[i-1].Score
            temp2 := peoples[i-1].Id
            peoples[i-1].Score=peoples[i].Score
            peoples[i-1].Id = peoples[i].Id
            peoples[i].Score = temp1
            peoples[i].Id = temp2
        }
    }
    return peoples
}

自己测的用例倒没什么问题,但机考的用例没全通过,只通过80%,不知道哪出了问题

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容