⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pack1.cpp

📁 01背包问题 用算法分之方法解决的问题
💻 CPP
字号:
#include <stdio.h>
#define W 1000
#define N 1000
int V[N][W];

typedef struct data
{
    int vaule;
    int weight;
}goods;

int returnmax(int a, int b)
{
    return (a > b ? a : b);
}

int KNAPSACK(goods *P, int a, int s)
{
    
    int i,j,mv;
    for(i = 0; i < a; i++)
        V[i][0] = 0;
    for(j = 0; j < s; j++)
        V[0][j] = 0;
    for(i = 1; i <= a; i++)
        for(j = 1; j <= s; j++)
        {
            V[i][j] = V[i - 1][j];
            if(P[i].weight <= j)
                V[i][j] = returnmax(V[i][j],V[i - 1][j - P[i].weight] + P[i].vaule);
        }
    mv = V[a][s];
    return mv;
}

int main()
{
    int mostvalue,amount,size,i;
    goods A[N]={0};
    printf("Input how much the goods have: ");
    scanf("%d",&amount);
    printf("Input the size of the bag: ");
    scanf("%d",&size);
    printf("Input the data of the goods: ");
    for(i = 0; i < amount; i++)
        scanf("%d %d",&A[i].vaule,&A[i].weight);
    mostvalue = KNAPSACK(A,amount,size);
    printf("%d",mostvalue);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -