80092 - 2026挑战赛入门组46-50
统计题目(材料题)
给定不同面额的硬币和一个总金额,每种硬币可以使用无限次。求使用硬币组成总金额所需的最少硬币个数。如果无法组成总金额,返回-1。
01 #include <iostream>
02 #include <cstring>
03 #include <algorithm>
04 using namespace std;
05
06 const int M = 10005;
07 const int INF = ① ;
08
09 int glod[105];
10 int dp[M];
11
12 int main() {
13 int n, money;
14 cin >> n >> money;
15 for (int i = 1; i <= n; i++) {
16 cin >> glod[i];
17 }
18 memset(dp, INF, sizeof(dp));
19 ②;
20 for (int i = 1; i <= n; i++) {
21 ③{
22 if (dp[j - glod[i]] != INF) {
23 dp[j] =④;
24 }
25 }
26 }
27 int ans = ⑤;
28 if (ans == INF)
29 ans = -1;
30 cout << ans << endl;
31 return 0;
32 }
||

关注我们