5个回答
展开全部
编程求全部解,如下:
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;
int num[5] = {1,2,3,4,5};
char op[4] = {'+','-','*','/'};
char *pri[5] = {"123","132","213","231","321"};
double compute(double num1, char op, double num2)
{
switch (op)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
}
return 0;
}
int main()
{
int i, j, k, p;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
for (k = 0; k < 4; k++)
for (p = 0; p < 5; p++)
{
switch (p)
{
case 0:
if (fabs(compute(compute(compute(num[0],op[i],num[1]), op[j], num[2]), op[k], num[3]) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 1:
if (fabs(compute(compute(num[0],op[i],num[1]), op[j], compute(num[2], op[k], num[3])) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 2:
if (fabs(compute(compute(num[0],op[i],compute(num[1], op[j], num[2])), op[k], num[3]) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 3:
if (fabs(compute(num[0],op[i],compute(compute(num[1], op[j], num[2]), op[k], num[3])) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 4:
if (fabs(compute(num[0],op[i],compute(num[1], op[j], compute(num[2], op[k], num[3]))) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
}
}
return 0;
}
运行结果:
0 2 1 0
0 3 0 0
1 1 2 3
对应的所有解为:
(1+2)*3-4=5
(1+2)/3+4=5
1-(2-3)*4=5
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;
int num[5] = {1,2,3,4,5};
char op[4] = {'+','-','*','/'};
char *pri[5] = {"123","132","213","231","321"};
double compute(double num1, char op, double num2)
{
switch (op)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
}
return 0;
}
int main()
{
int i, j, k, p;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
for (k = 0; k < 4; k++)
for (p = 0; p < 5; p++)
{
switch (p)
{
case 0:
if (fabs(compute(compute(compute(num[0],op[i],num[1]), op[j], num[2]), op[k], num[3]) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 1:
if (fabs(compute(compute(num[0],op[i],num[1]), op[j], compute(num[2], op[k], num[3])) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 2:
if (fabs(compute(compute(num[0],op[i],compute(num[1], op[j], num[2])), op[k], num[3]) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 3:
if (fabs(compute(num[0],op[i],compute(compute(num[1], op[j], num[2]), op[k], num[3])) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
case 4:
if (fabs(compute(num[0],op[i],compute(num[1], op[j], compute(num[2], op[k], num[3]))) - num[4]) <= 1e-5)
printf("%d %d %d %d\n", i, j, k, p);
break;
}
}
return 0;
}
运行结果:
0 2 1 0
0 3 0 0
1 1 2 3
对应的所有解为:
(1+2)*3-4=5
(1+2)/3+4=5
1-(2-3)*4=5
展开全部
(1+2)÷3+4=5
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
(3*4)/2-1=5
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
-1*(2-3)+4=5
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询