GO array exercise

Keywords: Go

Title Requirements:

In diving competition, 8 judges score, the highest score and the lowest score are removed from the athlete's score, and the average score of the remaining 6 scores is the final score

(1) please find out the judges with the highest score and the lowest score

(2) find out the best and worst judges. The best judges have the smallest gap in final scores, and the worst judges have the largest gap in final scores

Analysis:

To design a function to get the highest score, the lowest score and the average score, it is necessary to consider that there are multiple lowest scores and the highest score

Find the best referee and the worst referee, use abs() and slice completion to transfer the absolute value into the slice, and then traverse


Source code:

package main

//In diving competition, 8 judges give marks. The highest score and the lowest score are removed from the athlete's score. The average score of the remaining 6 scores is the final score
//Please find out the judges with the highest score and the lowest score
//Find the best judges and the worst judges. The best judges have the smallest gap in final scores, and the worst judges have the largest gap in final scores
//Analysis:
//To design a function to get the highest score, the lowest score and the average score, it is necessary to consider the existence of multiple lowest scores and the highest scores
//Find the best referee and the worst referee, use abs() and slice completion to pass the absolute value into the slice and then traverse it

import (
   "fmt"
   "math"
)
func max(array  *[8]float64) (mx float64,mi float64,avg float64){
   max := 0.0
   for i := 0; i < len(array); i++ {
      if (*array)[i] > max {
         max = (*array)[i]
      }
   }
   min := max
   for i := 0; i < len(array); i++ {
      if (*array)[i] < min  {
         min = (*array)[i]
      }
   }
   mx = max
   mi = min
   sum := 0.0
   maxcount,mincount :=0.0,0.0
   for i := 0; i < len(array); i++ {
       //Judge the number of times that the maximum value and the minimum value appear ﹣ 1 time, directly remove ﹣ multiple times is needed to add
      if (*array)[i] == max {
         maxcount +=1
      }
      if (*array)[i] == min{
         mincount +=1
      }
      //Calculate the sum of the maximum value and the minimum value that do not contain any one time
      if (*array)[i] != max && (*array)[i] != min{
         sum += (*array)[i]
      }

   }
   //fmt.Println(maxcount,mincount)
   //Processing has multiple maximum or minimum values
   if  mincount > 1.0 && maxcount > 1.0{
      sum += (max*(maxcount-1)+min*(mincount-1))
   }else if mincount > 1.0 && maxcount == 1.0{
      sum += (min*(mincount-1))
   }else if mincount ==1.0  && maxcount > 1.0{
      sum += (max*(maxcount-1))
   }else {
       sum += 0
   }
   avg = sum/6.0
   return  mx,min,avg
}

func Best(array1  *[8]float64, arry2 []float64, avg float64) {

   for i := 0; i < len(array1); i++ {
      arry2 = append(arry2, math.Abs((*array1)[i]-avg))
   }
   max  := 0.0
   for j :=0;j < len(arry2);j++{
      if arry2[j] > max{
         max = arry2[j]
      }
   }
   min := max
   for i := 0; i < len(arry2); i++ {
      if arry2[i] < min  {
         min = arry2[i]
      }
   }
   for i := 0; i < len(arry2); i++ {
      if arry2[i] == min {
         fmt.Printf("The best rater is No%v Position referee,Score:%v Difference from average score%v\n",i+1,(*array1)[i],min)
      }
   }

   for i := 0; i < len(arry2); i++ {
      if arry2[i] == max  {
         fmt.Printf("The highest score difference is%v Position referee,Score:%v Difference from average score%v\n",i+1,(*array1)[i],max)
      }
   }
}


var Socre [8]float64
var Socreabs = make([]float64,0,0)
func main() {
   //Enter scores for 8 referees
   for  i := 0;i<len(Socre);i++{
      fmt.Printf("Please input number 1.%v Scores of judges:\n",i+1)
      fmt.Scanln(&Socre[i])
   }

   max,min,avg :=max(&Socre)
   fmt.Println(Socre)
   fmt.Printf("The highest score is:%v,The minimum score is:%v The average score is:%v\n",max,min,avg)
   //Judges who know the maximum score, the minimum score, find the maximum score and the minimum score
   for k :=0;k<len(Socre);k++{
      if Socre[k] == max{
         fmt.Printf("The highest score is:%v,The first%v Position referee\n",max,k+1)
      }else if Socre[k] == min {
         fmt.Printf("Minimum score:%v,The first%v Position referee\n",min,k+1)
      }
   }
   Best(&Socre,Socreabs,avg)


}


Result


Posted by beefy23 on Tue, 03 Dec 2019 16:09:41 -0800