题解 | #点菜问题#
点菜问题
https://www.nowcoder.com/practice/b44f5be34a9143aa84c478d79401e22a
#include <iostream> using namespace std; const int MAXN = 101; const int MAXP = 1001; // dp数组在主函数外定义,元素自动初始化0 int dp[MAXN][MAXP], v[MAXN], w[MAXN]; int main(){ int c,n; while (cin>>c>>n){ for (int i = 1; i <= n; ++i) { cin>>w[i]>>v[i]; } // 双层遍历 for (int i = 1; i <= n; ++i) { for (int j = 1; j <= c; ++j) { if (j>=w[i]){ dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i]); } else{ dp[i][j] = dp[i-1][j]; } } } cout<<dp[n][c]<<endl; } return 0; }