golang 实现距离幂算法
func main() { var test []Pow var x1 Pow x1.distance = 110 x1.grade = 0.31 var x2 Pow x2.distance = 80 x2.grade = 0.25 var x3 Pow x3.distance = 120 x3.grade = 0.18 var x4 Pow x4.distance = 70 x4.grade = 0.21 test = append(test, x1) test = append(test, x2) test = append(test, x3) test = append(test, x4) var distance_sum float64 var powList []Pow grade:= float64(0) for _,v := range test{ var temp Pow distance_sum+=1/math.Pow(v.distance,2) temp.distance = 1/math.Pow(v.distance,2) temp.grade = v.grade powList = append(powList, temp) } for _,v1:=range powList{ grade+=v1.grade*(v1.distance/distance_sum) } beego.Debug(grade) }