谁能用3 7 8 9加减乘除使结果等于24
3个回答
展开全部
请输入四个数:
3 7 8 9
请输入计算值:
24
满足条件的有:
3*(7-8+9)=24
3*((7-8)+9)=24
3*(7-(8-9))=24
3*(7+9-8)=24
3*((7+9)-8)=24
3*(7+(9-8))=24
3*(9+7-8)=24
3*((9+7)-8)=24
3*(9+(7-8))=24
3*(9-8+7)=24
3*((9-8)+7)=24
3*(9-(8-7))=24
(7-8+9)*3=24
((7-8)+9)*3=24
(7-(8-9))*3=24
(7+9-8)*3=24
((7+9)-8)*3=24
(7+(9-8))*3=24
(9+7-8)*3=24
((9+7)-8)*3=24
(9+(7-8))*3=24
(9-8+7)*3=24
((9-8)+7)*3=24
(9-(8-7))*3=24
总共24个解!
Continue(y/n)?
C语言计算24点程序:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
double fun1();
double fun2();
double fun3();
double fun4();
char calc[64];
int n;
double fun1()
{
double t;
t=fun2();
while((calc[n]=='+')||(calc[n]=='-'))
{
switch(calc[n])
{
case '+':n++,t=t+fun2();break;
case '-':n++,t=t-fun2();break;
}
}
return(t);
}
double fun2()
{
double t;
t=fun3();
while((calc[n]=='*')||(calc[n]=='/'))
{
switch(calc[n])
{
case '*':n++,t=t*fun3();break;
case '/':n++,t=t/fun3();break;
}
}
return(t);
}
double fun3()
{
char num[16];
int i=0;
double t=0;
if(calc[n]=='(')
{
n++,t=fun1(),n++;
}
else if(fun4())
{
while(fun4())
{
num[i++]=calc[n++];
}
num[i]='\0';
t=atof(num);
}
return(t);
}
double fun4()
{
if(((calc[n]>='0'&&calc[n]<='9')||(calc[n]=='.'))||(n>0&&(calc[n-1]=='+'||calc[n-1]
=='-'||calc[n-1]=='*'||calc[n-1]=='/'||calc[n-1]=='^'||calc[n-1]=='(')))
return(1);
else
return(0);
}
int main(void)
{
int i,j,k,w,x,y,z,f,g,flag;
char sym[4]="+-*/",exp[24][32];
float a,b,c,d,e,num[5];
loop:
printf("请输入四个数:\n");
scanf("%f %f %f %f",&a,&b,&c,&d);
printf("\n请输入计算值:\n");
scanf("%f",&e);
printf("\n满足条件的有:\n");
f=g=0,flag=0,num[0]=a,num[1]=b,num[2]=c,num[3]=d;
for(w=0;w<=3;w++)
for(x=0;x<=3;x++)
{
if(x!=w)
{
for(y=0;y<=3;y++)
{
if(y!=w&&y!=x)
{
for(z=0;z<=3;z++)
{
if(z!=w&&z!=x&&z!=y)
{
sprintf(exp[f],"%g %g %g %g",num[w],num[x],num[y],num[z]);
for(g=0;g<f;g++)
{
if(strcmp(exp[g],exp[f])==0) {f--;g=0;break;}
}
if(g!=0||(f==0&&flag==0))
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
for(k=0;k<=3;k++)
{
n=0;
sprintf(calc,"%g%c%g%c%g%c%g\0",num[w],sym[i],num[x],sym[j],num[y],sym
[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c%g)%c%g%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c(%g%c%g)%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c%g%c(%g%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c%g%c%g)%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c(%g%c%g%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"((%g%c%g)%c%g)%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c(%g%c%g))%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c%g)%c(%g%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c((%g%c%g)%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c(%g%c(%g%c%g))\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
}
f++;
}
}
}
}
}
}
printf("总共%d个解!\n",flag);
printf("\nContinue(y/n)?");
switch(getch())
{
case 'y':{system("cls");goto loop;}
case 'n':
default :exit(0);
}
getch();
return(0);
}
3 7 8 9
请输入计算值:
24
满足条件的有:
3*(7-8+9)=24
3*((7-8)+9)=24
3*(7-(8-9))=24
3*(7+9-8)=24
3*((7+9)-8)=24
3*(7+(9-8))=24
3*(9+7-8)=24
3*((9+7)-8)=24
3*(9+(7-8))=24
3*(9-8+7)=24
3*((9-8)+7)=24
3*(9-(8-7))=24
(7-8+9)*3=24
((7-8)+9)*3=24
(7-(8-9))*3=24
(7+9-8)*3=24
((7+9)-8)*3=24
(7+(9-8))*3=24
(9+7-8)*3=24
((9+7)-8)*3=24
(9+(7-8))*3=24
(9-8+7)*3=24
((9-8)+7)*3=24
(9-(8-7))*3=24
总共24个解!
Continue(y/n)?
C语言计算24点程序:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
double fun1();
double fun2();
double fun3();
double fun4();
char calc[64];
int n;
double fun1()
{
double t;
t=fun2();
while((calc[n]=='+')||(calc[n]=='-'))
{
switch(calc[n])
{
case '+':n++,t=t+fun2();break;
case '-':n++,t=t-fun2();break;
}
}
return(t);
}
double fun2()
{
double t;
t=fun3();
while((calc[n]=='*')||(calc[n]=='/'))
{
switch(calc[n])
{
case '*':n++,t=t*fun3();break;
case '/':n++,t=t/fun3();break;
}
}
return(t);
}
double fun3()
{
char num[16];
int i=0;
double t=0;
if(calc[n]=='(')
{
n++,t=fun1(),n++;
}
else if(fun4())
{
while(fun4())
{
num[i++]=calc[n++];
}
num[i]='\0';
t=atof(num);
}
return(t);
}
double fun4()
{
if(((calc[n]>='0'&&calc[n]<='9')||(calc[n]=='.'))||(n>0&&(calc[n-1]=='+'||calc[n-1]
=='-'||calc[n-1]=='*'||calc[n-1]=='/'||calc[n-1]=='^'||calc[n-1]=='(')))
return(1);
else
return(0);
}
int main(void)
{
int i,j,k,w,x,y,z,f,g,flag;
char sym[4]="+-*/",exp[24][32];
float a,b,c,d,e,num[5];
loop:
printf("请输入四个数:\n");
scanf("%f %f %f %f",&a,&b,&c,&d);
printf("\n请输入计算值:\n");
scanf("%f",&e);
printf("\n满足条件的有:\n");
f=g=0,flag=0,num[0]=a,num[1]=b,num[2]=c,num[3]=d;
for(w=0;w<=3;w++)
for(x=0;x<=3;x++)
{
if(x!=w)
{
for(y=0;y<=3;y++)
{
if(y!=w&&y!=x)
{
for(z=0;z<=3;z++)
{
if(z!=w&&z!=x&&z!=y)
{
sprintf(exp[f],"%g %g %g %g",num[w],num[x],num[y],num[z]);
for(g=0;g<f;g++)
{
if(strcmp(exp[g],exp[f])==0) {f--;g=0;break;}
}
if(g!=0||(f==0&&flag==0))
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
for(k=0;k<=3;k++)
{
n=0;
sprintf(calc,"%g%c%g%c%g%c%g\0",num[w],sym[i],num[x],sym[j],num[y],sym
[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c%g)%c%g%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c(%g%c%g)%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c%g%c(%g%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c%g%c%g)%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c(%g%c%g%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"((%g%c%g)%c%g)%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c(%g%c%g))%c%g\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"(%g%c%g)%c(%g%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c((%g%c%g)%c%g)\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
n=0;
sprintf(calc,"%g%c(%g%c(%g%c%g))\0",num[w],sym[i],num[x],sym[j],num
[y],sym[k],num[z]);
{
num[4]=fun1();
if(num[4]==e) printf("%s=%g\n",calc,num[4]),flag++;
}
}
f++;
}
}
}
}
}
}
printf("总共%d个解!\n",flag);
printf("\nContinue(y/n)?");
switch(getch())
{
case 'y':{system("cls");goto loop;}
case 'n':
default :exit(0);
}
getch();
return(0);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1: 3 × (7 - 8 + 9)
2: 3 × ((7 - 8) + 9)
3: 3 × (7 - (8 - 9))
4: 3 × (7 + 9 - 8)
5: 3 × ((7 + 9) - 8)
6: 3 × (7 + (9 - 8))
7: 3 × (9 + 7 - 8)
8: 3 × ((9 + 7) - 8)
9: 3 × (9 + (7 - 8))
10: 3 × (9 - 8 + 7)
11: 3 × ((9 - 8) + 7)
12: 3 × (9 - (8 - 7))
13: (7 - 8 + 9) × 3
14: ((7 - 8) + 9) × 3
15: (7 - (8 - 9)) × 3
16: (7 + 9 - 8) × 3
17: ((7 + 9) - 8) × 3
18: (7 + (9 - 8)) × 3
19: (9 + 7 - 8) × 3
20: ((9 + 7) - 8) × 3
21: (9 + (7 - 8)) × 3
22: (9 - 8 + 7) × 3
23: ((9 - 8) + 7) × 3
24: (9 - (8 - 7)) × 3
2: 3 × ((7 - 8) + 9)
3: 3 × (7 - (8 - 9))
4: 3 × (7 + 9 - 8)
5: 3 × ((7 + 9) - 8)
6: 3 × (7 + (9 - 8))
7: 3 × (9 + 7 - 8)
8: 3 × ((9 + 7) - 8)
9: 3 × (9 + (7 - 8))
10: 3 × (9 - 8 + 7)
11: 3 × ((9 - 8) + 7)
12: 3 × (9 - (8 - 7))
13: (7 - 8 + 9) × 3
14: ((7 - 8) + 9) × 3
15: (7 - (8 - 9)) × 3
16: (7 + 9 - 8) × 3
17: ((7 + 9) - 8) × 3
18: (7 + (9 - 8)) × 3
19: (9 + 7 - 8) × 3
20: ((9 + 7) - 8) × 3
21: (9 + (7 - 8)) × 3
22: (9 - 8 + 7) × 3
23: ((9 - 8) + 7) × 3
24: (9 - (8 - 7)) × 3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
3*[(9+7)-8]=24
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询