首页 >烘焙

算24点程序:面向过程与面向对象的C++ (2)

2019-10-16 22:37:33 | 来源: 烘焙

3、面向过程的C实现

这是 csdn 算法论坛前版主海星的代码,程序非常简练、精致:

#include

#include

#include

using namespace std;

const double PRECISION = 1E-6;

const int COUNT_OF_NUMBER = 4;

const int NUMBER_TO_BE_CAL = 24;

double number[COUNT_OF_NUMBER];

string expression[COUNT_OF_NUMBER];

bool Search(int n)

{

if (n == 1) {

if ( fabs(number[0] - NUMBER_TO_BE_CAL) < PRECISION ) {

cout << expression[0] << endl;

return true;

} else {

return false;

}

}

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

double a, b;

string expa, expb;

a = number[i];

b = number[j];

number[j] = number[n - 1];

expa = expression[i];

expb = expression[j];

expression[j] = expression[n - 1];

expression[i] = '(' + expa + '+' + expb + ')';

number[i] = a + b;

if ( Search(n - 1) ) return true;

expression[i] = '(' + expa + '-' + expb + ')';

number[i] = a - b;

if ( Search(n - 1) ) return true;

expression[i] = '(' + expb + '-' + expa + ')';

number[i] = b - a;

if ( Search(n - 1) ) return true;

expression[i] = '(' + expa + '*' + expb + ')';

number[i] = a * b;

if ( Search(n - 1) ) return true;

if (b != 0) {

expression[i] = '(' + expa + '/' + expb + ')';

number[i] = a / b;

if ( Search(n - 1) ) return true;

}

if (a != 0) {

expression[i] = '(' + expb + '/' + expa + ')';

number[i] = b / a;

if ( Search(n - 1) ) return true;

}

number[i] = a;

number[j] = b;

expression[i] = expa;

expression[j] = expb;

}

}

return false;

}

void main()

{

for (int i = 0; i < COUNT_OF_NUMBER; i++) {

char buffer[20];

int x;

cin >> x;

number[i] = x;

itoa(x, buffer, 10);

expression[i] = buffer;

}

if ( Search(COUNT_OF_NUMBER) ) {

cout << "Success." << endl;

} else {

cout << "Fail." << endl;

}

}

使用任一个 c++ 编译器编译即可。

查看本文来源

泰州能治疗牛皮癣的医院
宜宾治疗盆腔炎医院
商洛治疗月经不调费用
赤峰哪家医院治疗性病
桂林癫痫病医院

猜你喜欢