典型的递归算法,C++参考代码:
#include
using namespace std;
const unsigned long RESULT = 1411263;
const unsigned long num1 = { 29126, 43689, 29126, 46602, 46602, 87378, 52427, 52427, 19417, 582522, 679609, 728153, 825240, 95145 };
/* 以上把已知数*100转为整型,提高运算速度与精度 */
unsigned long num2, maxnum;
bool Function(int index)
{
if (index == 14)
{
unsigned long sum = 0;
for (int i = 0; i < 14; i++) sum += num1[i] * num2[i];
if (sum == RESULT)
{
for (int i = 0; i < 14; i++) cout << num2[i] << " ";
cout << endl;
}
return sum <= RESULT;
}
else
{
for (int i = index; i < 14; i++) num2[i] = 0;
while (num2[index] <= maxnum[index])
{
if (!Function(index + 1)) break;
num2[index]++;
}
return true;
}
}
int main()
{
for (int i = 0; i < 14; i++) maxnum[i] = RESULT / num1[i];
Function(0);
return 0;
}
没编译调试,不保证正确,仅供参考,估计真跑起来很费时。