韩顺平 尚硅谷 Array题答案(Golang)

FredricZhu · · 910 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

image.png

image.png

image.png

image.png

作业1答案

package main

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

func reversePrint(arr [10]int) {
    _len := len(arr)
    for i := 0; i < _len/2; i++ {
        arr[i], arr[_len-1-i] = arr[_len-1-i], arr[i]
    }

    fmt.Println(arr)
}

func main() {
    var arr [10]int
    // 随机生成10个1-100的整数
    rand.Seed(int64(time.Now().Nanosecond()))
    for i := 0; i < len(arr); i++ {
        var curInt int
        for curInt == 0 {
            curInt = rand.Intn(101)
        }
        arr[i] = curInt
    }
    // 顺序打印
    fmt.Println(arr)

    // 倒序打印
    reversePrint(arr)

    // 再打印一次arr, 验证值没变
    fmt.Println(arr)

    // 求平均值,最大值和最小值的下标
    sum := 0.0
    maxIdx := 0
    maxVal := arr[0]
    minIdx := 0
    minVal := arr[0]
    for i := 0; i < len(arr); i++ {
        if arr[i] > maxVal {
            maxVal = arr[i]
            maxIdx = i
        }
        if arr[i] < minVal {
            minVal = arr[i]
            minIdx = i
        }
        sum += float64(arr[i])
    }

    fmt.Printf("平均值为: %v, 最大值的索引: %v, 最小值的索引: %v\n", sum/float64(len(arr)), maxIdx, minIdx)

    index := -1
    for i := 0; i < len(arr); i++ {
        if arr[i] == 55 {
            index = i
            break
        }
    }
    if index != -1 {
        fmt.Printf("存在%v, 索引为: %v\n", 55, index)
    } else {
        fmt.Printf("不存在%v\n", 55)
    }
}

作业2答案

package main

import (
    "fmt"
)

func insertEle(arr []int, ele int, l int, r int) []int {
    left := l
    right := r

    if left > right {

        arrLeft := arr[0 : right+1]
        arrRight := arr[right+1:]
        retArr := make([]int, 0)
        retArr = append(retArr, arrLeft...)
        retArr = append(retArr, ele)
        retArr = append(retArr, arrRight...)
        return retArr
    }

    middle := (left + right) / 2
    if arr[middle] == ele {
        arrLeft := arr[0 : middle+1]
        arrRight := arr[middle+1:]
        retArr := make([]int, 0)
        retArr = append(retArr, arrLeft...)
        retArr = append(retArr, ele)
        retArr = append(retArr, arrRight...)
        return retArr
    } else if arr[middle] > ele {
        right = middle - 1
        return insertEle(arr, ele, left, right)
    } else {
        left = middle + 1
        return insertEle(arr, ele, left, right)
    }
}

func main() {
    arr := []int{0, 2, 4, 6, 8, 10, 12}
    ret := insertEle(arr, 8, 0, len(arr)-1)
    fmt.Println(ret)
}

作业3答案

package main

import (
    "fmt"
)

func main() {
    var arr [3][4]int

    for i := 0; i < len(arr); i++ {
        for j := 0; j < len(arr[i]); j++ {
            fmt.Printf("请输入第 %v行 %v列的数值:\n", i+1, j+1)
            fmt.Scanln(&arr[i][j])
        }
    }

    for i := 0; i < len(arr); i++ {
        //将第一行和最后一行赋值为0
        if i == 0 || i == (len(arr)-1) {
            for j := 0; j < len(arr[i]); j++ {
                arr[i][j] = 0
            }
        }
    }

    //对列遍历
    for i := 0; i < len(arr[0]); i++ {
        //将第0列和最后一列置为0
        if i == 0 || i == (len(arr[0])-1) {
            for j := 0; j < len(arr); j++ {
                arr[j][i] = 0
            }
        }
    }

    fmt.Println(arr)

    fmt.Println(arr)
}

作业4答案

package main

import (
    "fmt"
)

func main() {
    var arr = [4][4]int{
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16},
    }

    //逐列遍历
    for i := 0; i < len(arr[0]); i++ {
        arr[0][i], arr[3][i] = arr[3][i], arr[0][i]
        arr[1][i], arr[2][i] = arr[2][i], arr[1][i]
    }

    fmt.Println(arr)
}

作业5答案

package main

import (
    "fmt"
)

func main() {
    arr := [5]int{1, 3, 5, 7, 9}
    _len := len(arr)

    for i := 0; i < _len/2; i++ {
        arr[i], arr[_len-1-i] = arr[_len-1-i], arr[i]
    }

    fmt.Println(arr)
}

作业6答案

package main

import (
    "fmt"
)

func seqSearch(arr [10]string, ele string) []int {
    var res = make([]int, 0)
    for i := 0; i < len(arr); i++ {
        if ele == arr[i] {
            res = append(res, i)
        }
    }
    return res
}

func main() {
    arr := [10]string{"AA", "cc", "BB", "AA", "aa", "zz", "MM", "cc", "dd", "ee"}
    res := seqSearch(arr, "cc")
    fmt.Println(res)
}

作业7答案

package main

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

func binarySearch(arr [10]int, ele int, l int, r int) int {
    left := l
    right := r

    if left > right {
        return -1
    }

    middle := (left + right) / 2
    if arr[middle] == ele {
        return middle
    } else if arr[middle] > ele {
        right = middle - 1
        return binarySearch(arr, ele, left, right)
    } else {
        left = middle + 1
        return binarySearch(arr, ele, left, right)
    }
}

func main() {
    // 生成随机整数数组
    rand.Seed(int64(time.Now().Nanosecond()))
    var intArr [10]int
    _len := len(intArr)
    for i := 0; i < _len; i++ {
        var tmpVal = rand.Intn(101)
        for tmpVal == 0 {
            tmpVal = rand.Intn(101)
        }
        intArr[i] = tmpVal
    }

    fmt.Println(intArr)

    // 冒泡排序

    for i := 0; i < _len-1; i++ {
        for j := 0; j < _len-1-i; j++ {
            if intArr[j] > intArr[j+1] {
                intArr[j], intArr[j+1] = intArr[j+1], intArr[j]
            }
        }
    }

    fmt.Println(intArr)

    //使用二分查找查找90
    index := binarySearch(intArr, 90, 0, len(intArr)-1)
    if index != -1 {
        fmt.Printf("找到该数: %v, 索引为: %v\n", 90, index)
    } else {
        fmt.Printf("找不到该数: %v\n", 90)
    }
}

作业8答案

package main

import (
    "fmt"
)

type Value struct {
    Val int
    Idx int
}

func findMaxMinAndIndex(arr [5]int) map[string]Value {
    var ret = make(map[string]Value, 0)
    var maxIdx = 0
    var maxVal = arr[0]
    var minIdx = 0
    var minVal = arr[0]

    for i := 0; i < len(arr); i++ {
        if arr[i] > maxVal {
            maxVal = arr[i]
            maxIdx = i
        }

        if arr[i] < minVal {
            minVal = arr[i]
            minIdx = i
        }
    }
    ret["max"] = Value{
        Val: maxVal,
        Idx: maxIdx,
    }
    ret["min"] = Value{
        Val: minVal,
        Idx: minIdx,
    }
    return ret
}

func main() {
    arr := [5]int{9, 8, 3, 7, 5}
    ret := findMaxMinAndIndex(arr)
    if val, ok := ret["max"]; ok {
        fmt.Printf("最大值为: %v,索引为: %v\n", val.Val, val.Idx)
    }

    if val, ok := ret["min"]; ok {
        fmt.Printf("最小值为: %v,索引为: %v\n", val.Val, val.Idx)
    }
}

作业9答案

package main

import (
    "fmt"
)

func main() {
    arr := [8]int{9, 5, 6, 7, 8, 10, 4, 3}

    _len := len(arr)
    sum := 0.0
    for i := 0; i < _len; i++ {
        sum += float64(arr[i])
    }
    avg := sum / float64(_len)

    greaterCnt := 0
    lessCnt := 0

    for i := 0; i < _len; i++ {
        if float64(arr[i]) > avg {
            greaterCnt++
        }

        if float64(arr[i]) < avg {
            lessCnt++
        }
    }

    fmt.Printf("平均值为: %v, 大于平均值的个数为: %v, 小于平均值的个数为: %v\n",
        avg, greaterCnt, lessCnt)

}

作业10答案

package main

import (
    "fmt"
    "math"
)

func main() {
    var scores = [8]int{9, 7, 8, 5, 6, 10, 4, 5}

    // 找出最高分和最低分评委
    maxIdx := 0
    maxVal := scores[0]
    minIdx := 0
    minVal := scores[0]

    _len := len(scores)
    for i := 0; i < _len; i++ {
        if scores[i] > maxVal {
            maxVal = scores[i]
            maxIdx = i
        }

        if scores[i] < minVal {
            minVal = scores[i]
            minIdx = i
        }
    }

    fmt.Printf("打最高分的评委是 %v, 打最低分的评委是 %v\n", maxIdx, minIdx)

    // 求最后得分
    finalScore := 0.0

    for i := 0; i < _len; i++ {
        if i != maxIdx && i != minIdx {
            finalScore += float64(scores[i])
        }
    }

    finalScore = finalScore / float64(_len-2)

    fmt.Printf("最后得分为: %v\n", finalScore)

    // 找出最佳评委和最差评委
    maxDiffIdx := 0
    maxDiff := math.Abs(finalScore - float64(scores[0]))
    minDiffIdx := 0
    minDiff := math.Abs(finalScore - float64(scores[0]))

    for i := 0; i < _len; i++ {
        diff := math.Abs(finalScore - float64(scores[i]))
        if diff > maxDiff {
            maxDiff = diff
            maxDiffIdx = i
        }

        if diff < minDiff {
            minDiff = diff
            minDiffIdx = i
        }
    }

    fmt.Printf("最佳评委为: %v, 误差为: %v\n", minDiffIdx, minDiff)
    fmt.Printf("最差评委为: %v, 误差为: %v\n", maxDiffIdx, maxDiff)
}


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:FredricZhu

查看原文:韩顺平 尚硅谷 Array题答案(Golang)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

910 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传