• 【Leetcode】国王挖金矿


    参考该文章

    https://www.cnblogs.com/henuliulei/p/10041737.html

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    
    int main (int argc, char** argv){
        
        int workerNumber = 10;
        int auNumber = 5;
        int maxVal = 0;
        int auVal[] = {100, 200, 150, 400, 300};
        int auCost[] = {3, 4, 3, 5, 5};
        bool choose[] = {false, false, false, false, false};
        int valueT[auNumber+1][workerNumber+1];
        std::memset(valueT, 0, sizeof(valueT));
        
        // initialize the valueT
        for(int i = 1; i <= auNumber; i++){
            for(int j = 1; j <= workerNumber; j++){
                if (j < auCost[i-1])
                    valueT[i][j] = valueT[i-1][j];
                else
                    valueT[i][j] = max(valueT[i-1][j], valueT[i-1][j-auCost[i-1]]+auVal[i-1]);
            }
        }
        
        for(int i = 0; i <= auNumber; i++){
            for(int j = 0; j <= workerNumber; j++){
                std::cout << valueT[i][j] << " ";
            }
            std::cout << std::endl;
        }
        
        // get the result
        int k = workerNumber;
        for(int i = auNumber; i > 0; i--){
            if(valueT[i][k] > valueT[i-1][k]){
                choose[i-1] = true;
                
                k = k - auCost[i-1];
            }        
        }
        
        for(int i = 0; i < auNumber; i++)
            cout << choose[i] << " ";
        
        return 0;
    }
  • 相关阅读:
    C++ 内置函数 判断字母、数字及大小写转换
    C++11 随机数 random
    rpc
    C++11 智能指针
    xargs 命令使用
    记录优秀的文章
    腾讯 测试开发
    struts2文件上传、下载、防止重复提交
    注解
    @RestController注解
  • 原文地址:https://www.cnblogs.com/gdut-gordon/p/11335112.html
Copyright © 2020-2023  润新知