题解 | #最小邮票数#
最小邮票数
https://www.nowcoder.com/practice/83800ae3292b4256b7349ded5f178dd1
#include <iostream> #include <cstring> using namespace std; const int N = 110, INF = 0x3f3f3f3f; int f[N], w[N]; int main(){ int n; int m; while(cin>>m){ memset(f, 0x3f, sizeof f); cin>>n; for(int i = 1; i <= n; i ++){ cin>>w[i]; } f[0] = 0; for(int i = 1; i <= n; i ++) for(int j = m; j >= w[i]; j --){ f[j] = min(f[j], f[j-w[i]] + 1); } if(f[m] != INF) cout<<f[m]<<endl; else cout<<0<<endl; } }
01背包