题解 | #最小邮票数#
最小邮票数
https://www.nowcoder.com/practice/83800ae3292b4256b7349ded5f178dd1
用dfs比较简单
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
//利用深度优先遍历
int m, n;
int ans = 100;
void DFS(int pos,int stamps[], int current,int c ) { // current表当前的价值,count表当前的数量
if (pos == n-1) return;
for (int i = pos+1; i < n; i++) //遍历其邻居
{
if (current+stamps[i] == m )
ans = min(ans, c+1);
if (current + stamps[i] < m)
{ //如果当前面额大于该邮票
DFS(i, stamps, current + stamps[i], c + 1);
}
}
}
int main() {
while (cin >> m >>n)
{
int stamps[25];
for (int i = 0; i < n; i++) {
cin >> stamps[i];
}
DFS(0, stamps, 0, 0);
if(ans==100) cout << "0"<<endl;
else
cout << ans << endl;
}
}

