内存池申请内存

1.  内存池申请内存:

输入, 第一行为 空闲内存值:份数 格式; 第二行为申请的内存,逗号分隔; 优先分配最小且合适的
64:2,1:128,256:3
63,129,255,255,255

输出,申请成功或失败
true,true,true,true,false
 
 
func main() {
    var a1 string
    var a2 string
    cacheList := make(map[int]int, 0)
    result := make([]bool, 0)

    n, err := fmt.Scan(&a1, &a2)
    if err != nil || n != 2 {
        fmt.Println("获取输入错误")
    }

    cacheArr := strings.Split(a1, ",")
    taskArr := strings.Split(a2, ",")
    for _, cache := range cacheArr {
        cacheCap := strings.Split(cache, ":")
        capacity, _ := strconv.Atoi(cacheCap[0])
        num, _ := strconv.Atoi(cacheCap[1])
        cacheList[capacity] = num

    }
    for _, task := range taskArr {
        flag := false
        taskCap, _ := strconv.Atoi(task)
        for cacheCap, num := range cacheList {
            if cacheCap >= taskCap && num >= 1 {
                flag = true
                cacheList[cacheCap] --
                break
            }
        }
        result = append(result, flag)
    }

    for _, res := range result {
        fmt.Println("result", res)
    }

}

 

posted @ 2022-03-21 22:21  Mr.peter  阅读(100)  评论(0编辑  收藏  举报