zoukankan      html  css  js  c++  java
  • 苹果

    苹果

    描述

    ctest有n个苹果,要将它放入容量为v的背包。给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值。

     
    输入
    有多组测试数据,每组测试数据第一行为2个正整数,分别代表苹果的个数n和背包的容量v,n、v同时为0时结束测试,此时不输出。接下来的n行,每行2个正整数,用空格隔开,分别代表苹果的大小c和价钱w。所有输入数字的范围大于等于0,小于等于1000。
    输出
    对每组测试数据输出一个整数,代表能放入背包的苹果的总价值。
    样例输入
    3 3
    1 1
    2 1
    3 1
    0 0
    样例输出
    2

     
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    #include <vector>
    #include <list>
    #include <memory.h>
    #include <string>
    #include <math.h>
    using namespace std;
      int f[1006][1006];
      int c[1006];
      int w[1006];
      int main()
      {
          int n,v;
        while(cin>>n>>v && n!=0 && v!=0)
        {
            memset(f,0,sizeof(f));
            for(int i=1;i<=n;i++)
            {
                cin>>c[i]>>w[i];
            }
        
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=v;j++)
                {
                    f[i][j] = f[i-1][j];
                    if(j>=c[i])
                f[i][j] = max(f[i-1][j],(f[i-1][j-c[i]]+w[i]));
                }
            }
    
            cout<<f[n][v]<<endl;
        }
    
         return 0;
    }
    
                
  • 相关阅读:
    newman
    集合自动化
    56. Merge Intervals
    55. Jump Game
    48. Rotate Image
    34. Search for a Range
    33. Search in Rotated Sorted Array
    16. 3Sum Closest
    15. 3Sum
    11. Container With Most Water
  • 原文地址:https://www.cnblogs.com/imwtr/p/4069569.html
Copyright © 2011-2022 走看看