你解題的思路錯啦,直接爆破可不是甚麼好習慣
程序代码:
#include <stdio.h>
char reversion(char signal) {
if (signal == '*') {
return '/';
} else if (signal == '/') {
return '*';
} else if (signal == '+') {
return '-';
} else if (signal == '-') {
return '+';
}
}
void get_equation(int A, int B, int C, int D, char x, char y, char z, char w, char* equ) {
x = reversion(x);
y = reversion(y);
z = reversion(z);
w = reversion(w);
sprintf(equ,"a=%d%c(%d%c(%d%c(%d%ca)))",D,w,B,z,C,y,A,x);
}
float solve(int step) {
float temp;
for (float x = -9; x < 9*step; x++) {
float ttemp;
ttemp = (8*step-(5*step+(9*x/21)) - x);
if (ttemp == 0) {
return x/step;
}
if (x > 1 && (ttemp * temp < 0)) {
break;
}
temp = ttemp;
}
solve(step*10);
}
float cal(float x, char opr, float y) {
if (opr == '*') {
return x*y;
} else if (opr == '/') {
return x/y;
} else if (opr == '+') {
return x+y;
} else if (opr == '-') {
return x-y;
}
}
int main() {
char equ[100] = {0};
char x = '*';
char y = '*';
char z = '+';
char w = '-';
int A = 21;
int B = 5;
int C = 9;
int D = 8;
float a, b, c, d;
get_equation(A, B, C, D, x, y, z, w, equ);
printf("%s\n",equ);
a = solve(1);
b = cal(A, reversion(x), a);
d = cal(C, reversion(y), b);
c = cal(B, reversion(z), d);
printf("a=%f\n", a);
printf("b=%f\n", b);
printf("c=%f\n", c);
printf("d=%f\n", d);
return 0;
}
代數解很多時候其實不難用