您的位置首页百科知识

编程求解14元一次方程,只有一个公式,求291.26*a+436.89*b+291.26*c+466.02*d+466.

编程求解14元一次方程,只有一个公式,求291.26*a+436.89*b+291.26*c+466.02*d+466.

典型的递归算法,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;

}

没编译调试,不保证正确,仅供参考,估计真跑起来很费时。