3 solutions

  • 3
    @ 2024-2-29 10:47:01
    //代码实现1
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1010,M=1010;
    int f[N][M],v[N],w[N];
    //状态表示 f[i][j]表示在前i个物品中选,体积不超过j的最大价值
    //状态计算 1.不选 f[i-1][j]
    //         2.选   f[i-1][j-v[i]]+w[i] 
    int main()
    {
        int n,V;
        cin>>n>>V;
        for(int i=1;i<=n;i++) //获取物品体积和价值 
        {
            cin>>v[i]>>w[i];
        }
        for(int i=1;i<=n;i++) //枚举物品 
        {
            for(int j=0;j<=V;j++) //枚举体积 
            {
            	f[i][j]=f[i-1][j];//不选当前物品
    			if(j>=v[i])
    			{
    				f[i][j]=max(f[i][j],f[i][j-v[i]]+w[i]); 
    				//          原来的   选了的 里面的最大值	
    			} 
            }
        }
        cout<<f[n][V];
        return 0;
    }
    
    //代码实现2
    #include<bits/stdc++.h>
    using namespace std;
    const int M=1010;
    int f[M];
    //状态表示 f[j]表示在前i个物品中选,体积不超过j的最大价值
    //状态计算 1.不选 f[j]
    //         2.选   [j-v[i]]+w[i] 
    int main()
    {
        int n,V;
        cin>>n>>V;
        for(int i=1;i<=n;i++) //枚举物品 
        {
        	int v,w;
        	cin>>v>>w;
            for(int j=v;j<=V;j++) //枚举体积 
            {
            	f[j]=max(f[j],f[j-v]+w); 
    			//     原来的   选了的 里面的最大值	
            }
        }
        cout<<f[V];
        return 0;
    }
    
    
    • 0
      @ 2025-4-19 19:30:39
      #include <bits/stdc++.h>
      using namespace std;
      const int N=1010;
      int f[N][N];
      int w[N],g[N]; 
      int main(){
      	int n,m;
      	cin>>n>>m;
      	for(int i=1;i<=n;i++)
      	{
      		cin>>w[i]>>g[i];
      	}
      	for(int i=1;i<=n;i++)
      	{
      		for(int j=1;j<=m;j++)
      		{
      			f[i][j]=f[i-1][j];
      			if(j>=w[i])f[i][j]=max(f[i][j],g[i]+f[i][j-w[i]]);
      		}
      	}
      	cout<<f[n][m];
      	return 0;
      }
      
      
      
      • -3
        @ 2024-7-23 14:59:20
        //代码实现1
        #include<bits/stdc++.h>
        using namespace std;
        const int N=1010,M=1010;
        int f[N][M],v[N],w[N];
        //状态表示 f[i][j]表示在前i个物品中选,体积不超过j的最大价值
        //状态计算 1.不选 f[i-1][j]
        //         2.选   f[i-1][j-v[i]]+w[i]
        int main()
        {
        int n,V;
        cin>>n>>V;
        for(int i=1;i<=n;i++) //获取物品体积和价值
        {
        cin>>v[i]>>w[i];
        }
        for(int i=1;i<=n;i++) //枚举物品
        {
        for(int j=0;j<=V;j++) //枚举体积
        {
        f[i][j]=f[i-1][j];//不选当前物品
        if(j>=v[i])
        {
        f[i][j]=max(f[i][j],f[i][j-v[i]]+w[i]);
        //          原来的   选了的 里面的最大值	
        }
        }
        }
        cout<<f[n][V];
        return 0;
        }
        
        //代码实现2
        #include<bits/stdc++.h>
        using namespace std;
        const int M=1010;
        int f[M];
        //状态表示 f[j]表示在前i个物品中选,体积不超过j的最大价值
        //状态计算 1.不选 f[j]
        //         2.选   [j-v[i]]+w[i] 
        int main()
        {
            int n,V;
            cin>>n>>V;
            for(int i=1;i<=n;i++) //枚举物品 
            {
            	int v,w;
            	cin>>v>>w;
                for(int j=v;j<=V;j++) //枚举体积 
                {
                	f[j]=max(f[j],f[j-v]+w); 
        			//     原来的   选了的 里面的最大值	
                }
            }
            cout<<f[V];
            return 0;
        }
        
        • 1

        Information

        ID
        989
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        5
        Tags
        (None)
        # Submissions
        42
        Accepted
        20
        Uploaded By