2-21 composite type Exercises

package main

import (
    "fmt"
    "math/rand"
    "time"
)

var names []string

func main() {
    AddStudent ( " Chi Master " , " ranks the fourth " , " bachelor " , " Click here to see " , " West poisonous " , " Bai Jingqi " , " stinking ninth category " , " Wang " )
    fmt.Println(names)
    scoreMap := TakeExam(names...)
    fmt.Println(scoreMap)
    showRanking(scoreMap)
}

func Addstudent(AddComers ...string) {
    names = append(names, AddComers...)
}

func TakeExam(name... string)(scoreMap map[string]int) {
    scoreMap = make(map[string]int)
    for _, name := range names{
        Randine: = Randine ( 100 )
        scoreMap[name] = randint
    }
    return
}

// for people inside the map sort 
FUNC showRanking (scoreMap map [ String ] int ) {
    names := make([]string, 0)
    for name ,_ :=range scoreMap{
        names = append(names, name)
    }


    for i := 0; i < len(names)-1; i++ {
        for j := i+1;j <= len(names) ;j++  {
            if scoreMap[names[j]] > scoreMap[names[i]]{
                names[i],names[j] = names[j],names[i]
            }
        }
    }

    for index, name :=range names{
        fmt.Printf("第%d名:%-6s \t %-3d\n",index+1,names[index],scoreMap[name])
    }
}

Rand func (n int ) int {
    time.Sleep(time.Nanosecond)
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    return r.Intn(n + 1)
}

 

Guess you like

Origin www.cnblogs.com/paad/p/11057724.html