大意: 面值为1分,2分,3分的硬币各有a,b,c枚,求不能用这些硬币表示的最小值。
分析:硬币能够表示的最大值max=1*a+2*b+5*c,计算1,2,3...max,max+1的系数是否为0,若0则不能表示。
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; int c1[10050], c2[10050]; int main() { int a, b, c; while (~scanf("%d%d%d", &a, &b, &c)) { if (a == 0 && b == 0 && c == 0) break; memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); for (int i = 0; i <= a; i++) c1[i] = 1; for (int i = 0; i <= a; i++) for (int j = 0; j <= b; j++) c2[i + 2 * j] += c1[i]; for (int i = 0; i <= (a + 2 * b); i++) { c1[i] = c2[i]; c2[i] = 0; } for (int i = 0; i <= (a + 2 * b); i++) for (int j = 0; j <= c; j++) c2[i + 5 * j] += c1[i]; for (int i = 0; i <= (a + 2 * b + 5 * c); i++) c1[i] = c2[i]; for (int i = 0; i <= (a + 2 * b + 5 * c+1); i++) if (c1[i] == 0) { printf("%d ",i); break; } } return 0; }