Go 数组解决选手打分问题

7a05ce7cb079c59f88a46efc70f26167bb6ed2e5.jpg

选手打分

假设一个场景,相亲现场中,10位小姐姐对1位小哥哥打分,打分区间为1-10分,且每位小姐姐的打分都不相等。现在需要去掉一个最高分和一个最低分后,得出8个打分样本并计算平均分。

代码

package main

import (
    "fmt"
    "math"
)

func swapTwo(a *int, b *int) {
    *a, *b = *b, *a
}

func main() {
    scoreList := [10]float64{8.5,9.1,9.6,10,9.3,9.7,9.2,9.5,8.9,8.8}
    var maxIndex,minIndex = -1,-1
    var maxScore,minScore = 0.0,11.0
    var i int
    var sumScore = 0.0
    
    for i=0;i<len(scoreList);i++ {
        if scoreList[i]>maxScore {
            maxIndex=i
            maxScore=scoreList[i]
        }

        if scoreList[i]<minScore {
            minIndex=i
            minScore=scoreList[i]
        }
    }    

    if maxScore<minScore {
        swapTwo(&maxIndex, &minIndex)
    }

    for i=maxIndex;i<len(scoreList)-1;i++ {
        scoreList[i] = scoreList[i+1];
    }

    for i=minIndex;i<len(scoreList)-1;i++ {
        scoreList[i] = scoreList[i+1];
    }

    for i=0;i<len(scoreList)-2;i++ {
        sumScore += scoreList[i];
    }
    fmt.Println(math.Round(sumScore / 8 * 100) / 100)
}

执行

$ go run avgScore.go
9.26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值