Mean、 median and mode

package main
import "fmt"
import "sort"
type IntSlice []int  
  
func (c IntSlice) Len() int {  
    return len(c)  
}  
func (c IntSlice) Swap(i, j int) {  
    c[i], c[j] = c[j], c[i]  
}  
func (c IntSlice) Less(i, j int) bool {  
    return c[i] < c[j]  
}  
func main() {
 //Enter your code here. Read input from STDIN. Print output to STDOUT
    var num int
    fmt.Scanln(&num)
    var arr = make(IntSlice, 0, num)
    for i := 0; i < num; i++{
        var t int
        fmt.Scanf("%d", &t)
        arr = append(arr, t)
    }
    sort.Sort(arr)
    var sum int
    for _, val := range arr{
        sum += val
    }
    var l = len(arr)
    var media float32
    if l&0x01 != 0{
        media = float32(arr[l/2])
    }else{
        media = (float32(arr[l/2-1]) + float32(arr[l/2]))/2
    }
    var maxValue = arr[0]
    var freq = 1
    var maxFreq = 1
    for i := 1; i < l; i++{
        if arr[i - 1] == arr[i]{
            freq += 1
            if maxFreq < freq{
                maxFreq = freq
                maxValue = arr[i]
            }
        }else{
            freq = 1
        }
    }
     fmt.Printf("%0.1f\n", float32(sum)/float32(l))
    fmt.Printf("%0.1f\n", media)
    fmt.Printf("%d\n", maxValue)  
    
}

你可能感兴趣的:(Mean、 median and mode)