暴力搜索即可,注意比较 double 大小要借助 eps,不能直接比较。
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-8;
int main()
{
double sum;
int A, B, C, D;
int a, b, c, d;
while (cin >> sum >> A >> B >> C >> D)
{
a = A; b = B; c = C; d = D;
bool flag = false;
for (int i = 0; i <= D; i++)
{
for (int j = 0; j <= C; j++)
{
for (int k = 0; k <= B; k++)
{
for (int m = 0; m <= A; m++)
{
if (fabs(m * 0.25 + k * 0.1 + j * 0.05 + i * 0.01 - sum) <= eps)
{
a = m; b = k; c = j; d = i;
flag = true;
}
}
if (flag == true)
break;
}
if (flag == true)
break;
}
if (flag == true)
break;
}
if (flag == true)
cout << a << " " << b << " " << c << " " << d << endl;
else
cout << "NO EXACT CHANGE" << endl;
}
return 0;
}
继续加油。