美文网首页
第03天(复合类型)_04

第03天(复合类型)_04

作者: lucas777 | 来源:发表于2019-12-11 20:35 被阅读0次

    18_切片和底层数组关系.go

    package main //必须有个main包
    
    import "fmt"
    
    func main() {
        a := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    
        //新切片
        s1 := a[2:5] //从a[2]开始,取3个元素
        s1[1] = 666
        fmt.Println("s1 = ", s1)
        fmt.Println("a = ", a)
    
        //另外新切片
        s2 := s1[2:7]
        s2[2] = 777
        fmt.Println("s2 = ", s2)
        fmt.Println("a = ", a)
    }
    

    19_append函数的使用.go

    package main //必须有个main包
    
    import "fmt"
    
    func main() {
        s1 := []int{}
        fmt.Printf("len = %d, cap = %d\n", len(s1), cap(s1))
        fmt.Println("s1 = ", s1)
    
        //在原切片的末尾添加元素
        s1 = append(s1, 1)
        s1 = append(s1, 2)
        s1 = append(s1, 3)
        fmt.Printf("len = %d, cap = %d\n", len(s1), cap(s1))
        fmt.Println("s1 = ", s1)
    
        s2 := []int{1, 2, 3}
        fmt.Println("s2 = ", s2)
        s2 = append(s2, 5)
        s2 = append(s2, 5)
        s2 = append(s2, 5)
        fmt.Println("s2 = ", s2)
    }
    

    20_append扩容特点.go

    package main //必须有个main包
    
    import "fmt"
    
    func main() {
        //如果超过原来的容量,通常以2倍容量扩容
        s := make([]int, 0, 1) //容量为1
        oldCap := cap(s)
        for i := 0; i < 20; i++ {
            s = append(s, i)
            if newCap := cap(s); oldCap < newCap {
                fmt.Printf("cap: %d ===> %d\n", oldCap, newCap)
    
                oldCap = newCap
            }
        }
    
    }
    

    21_copy的使用.go

    package main //必须有个main包
    
    import "fmt"
    
    func main() {
        srcSlice := []int{1, 2}
        dstSlice := []int{6, 6, 6, 6, 6}
    
        copy(dstSlice, srcSlice)
        fmt.Println("dst = ", dstSlice)
    }
    

    22_切片做函数参数.go

    package main //必须有个main包
    
    import "fmt"
    import "math/rand"
    import "time"
    
    func InitData(s []int) {
        //设置种子
        rand.Seed(time.Now().UnixNano())
    
        for i := 0; i < len(s); i++ {
            s[i] = rand.Intn(100) //100以内的随机数
        }
    }
    
    //冒泡排序
    func BubbleSort(s []int) {
        n := len(s)
    
        for i := 0; i < n-1; i++ {
            for j := 0; j < n-1-i; j++ {
                if s[j] > s[j+1] {
                    s[j], s[j+1] = s[j+1], s[j]
                }
            }
        }
    }
    
    func main() {
        n := 10
    
        //创建一个切片,len为n
        s := make([]int, n)
    
        InitData(s) //初始化数组
        fmt.Println("排序前: ", s)
    
        BubbleSort(s) //冒泡排序
        fmt.Println("排序后: ", s)
    }
    

    23_猜数字游戏.go

    package main //必须有个main包
    
    import "fmt"
    import "math/rand"
    import "time"
    
    func CreatNum(p *int) {
        //设置种子
        rand.Seed(time.Now().UnixNano())
    
        var num int
        for {
            num = rand.Intn(10000) //一定是4位数
            if num >= 1000 {
                break
            }
        }
    
        //fmt.Println("num = ", num)
    
        *p = num
    
    }
    
    func GetNum(s []int, num int) {
        s[0] = num / 1000       //取千位
        s[1] = num % 1000 / 100 //取百位
        s[2] = num % 100 / 10   //取百位
        s[3] = num % 10         //取个位
    }
    
    func OnGame(randSlice []int) {
        var num int
        keySlice := make([]int, 4)
    
        for {
            for {
                fmt.Printf("请输入一个4位数:")
                fmt.Scan(&num)
    
                // 999 < num < 10000
                if 999 < num && num < 10000 {
                    break
                }
    
                fmt.Println("请输入的数不符合要求")
            }
            //fmt.Println("num = ", num)
            GetNum(keySlice, num)
            //fmt.Println("keySlice = ", keySlice)
    
            n := 0
            for i := 0; i < 4; i++ {
                if keySlice[i] > randSlice[i] {
                    fmt.Printf("第%d位大了一点\n", i+1)
                } else if keySlice[i] < randSlice[i] {
                    fmt.Printf("第%d位小了一点\n", i+1)
                } else {
                    fmt.Printf("第%d位猜对了\n", i+1)
                    n++
                }
            }
    
            if n == 4 { //4位都猜对了
                fmt.Println("全部猜对!!!")
                break //跳出循环
            }
        }
    }
    
    func main() {
        var randNum int
    
        //产生一个4位的随机数
        CreatNum(&randNum)
        //fmt.Println("randNum: ", randNum)
    
        randSlice := make([]int, 4)
        //保存这个4位数的每一位
        GetNum(randSlice, randNum)
        //fmt.Println("randSlice = ", randSlice)
    
        /*
            n1 := 1234 / 1000 //取商
            //(1234 % 1000) //取余数,结果为234   234/100取商得到2
            n2 := 1234 % 1000 / 100
            fmt.Println("n1 = ", n1)
            fmt.Println("n2 = ", n2)
        */
    
        OnGame(randSlice) //游戏
    
    }
    

    相关文章

      网友评论

          本文标题:第03天(复合类型)_04

          本文链接:https://www.haomeiwen.com/subject/ohuagctx.html