大体思路
对abcd做循环,然后验证是否符合取值,如果符合就打印出来。
因为只能是正整数,所以取值范围其实可以缩小,比如436a+512b+1035c要是正好17700,d只能取0,取1就越界了。
代码
#include <iostream>
using namespace std;
int main()
{
for (int a = 0; a <= 17700 / 436; a++)
{
for (int b = 0; b <= (17700-436*a) / 512; b++)
{
for (int c = 0; c <= (17700-436*a-512*b) / 1035; c++)
{
for (int d = 0; d <= (17700-436*a-512*b-1035*c) / 1199; d++)
{
if (436 * a + 512 * b + 1035 * c + 1199 * d == 17700)
{
cout << "436 x " << a << " + 512 x " << b << " + 1035 x " << c << " + 1199 x " << d << "= 17700" << endl;
}
else if (436 * a + 512 * b + 1035 * c + 1200 * d == 17700)
{
cout << "436 x " << a << " + 512 x " << b << " + 1035 x " << c << " + 1200 x " << d << "= 17700" << endl;
}
}
}
}
}
return 0;
}
运行结果
436 x 1 + 512 x 22 + 1035 x 0 + 1200 x 5= 17700
436 x 6 + 512 x 2 + 1035 x 2 + 1199 x 10= 17700
436 x 6 + 512 x 12 + 1035 x 4 + 1200 x 4= 17700
436 x 11 + 512 x 2 + 1035 x 8 + 1200 x 3= 17700
436 x 17 + 512 x 2 + 1035 x 2 + 1199 x 6= 17700
436 x 21 + 512 x 12 + 1035 x 0 + 1200 x 2= 17700
436 x 26 + 512 x 2 + 1035 x 4 + 1200 x 1= 17700
436 x 28 + 512 x 2 + 1035 x 2 + 1199 x 2= 17700
|