输入四位整数,可以输出用四则运算的结果。(仅限+,-,*,/,括号)。
#include <iostream>
#include <cmath> using namespace std;
double ans2(char* c)
{
double a,b;
double sum;
char t;
a=c[2]-48;b=c[4]-48;
t=c[3];
// cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<endl;
if(t=='+')
{sum=a+b;}
if(t=='-')
{sum=a-b;}
if(t=='*')
{sum=a*b;}
if(t=='/')
{sum=a/b;}
//cout<<sum<<endl; a=sum;b=c[7]-48;
t=c[6];
// cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<endl;
if(t=='+')
{sum=a+b;}
if(t=='-')
{sum=a-b;}
if(t=='*')
{sum=a*b;}
if(t=='/')
{sum=a/b;} a=sum;b=c[10]-48;
t=c[9];
// cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<endl;
if(t=='+')
{sum=a+b;}
if(t=='-')
{sum=a-b;}
if(t=='*')
{sum=a*b;}
if(t=='/')
{sum=a/b;} return sum; } void fkind(char* a,int n,int i,char* t,char *p)
{
static int tot=0;
if(i==n)
{
for(int j=0;j<n;j++)
{
// cout<<t[j]<<" ";
p[tot++]=t[j];
}
// cout<<endl;
// cout<<endl<<tot++<<endl;
}
else if(i<n)
{
for(int j=0;j<4;j++)
{
t[i]=a[j];
fkind(a,n,i+1,t,p);
}
}
} void nkind(int* a,int n,int i,int* t,int *p)
{
static int tot=0;
if(i==n)
{
for(int j=0;j<n;j++)
{
// cout<<t[j]<<" ";
p[tot++]=t[j];
}
// cout<<endl;
// cout<<endl<<tot++<<endl;
}
else if(i<n)
{
for(int j=0;j<4;j++)
{
if(a[j]!=-1)
{
t[i]=a[j];
int s=a[j];
a[j]=-1;
nkind(a,n,i+1,t,p);
a[j]=s;
}
}
}
} void equation(int* a,char* b,char* c)
{
c[0]=c[1]='(';
c[5]=c[8]=')';
c[2]=a[0]+48;
c[4]=a[1]+48;
c[7]=a[2]+48;
c[10]=a[3]+48;
c[3]=b[0];
c[6]=b[1];
c[9]=b[2];
c[11]='\0';
} int main()
{
// char c[15];
int a[4];
int num[24][4];
int tn[4];
int* pn=num[0]; char str[64][3];
char cs[4]={'+','-','*','/'};
char tc[4];
char* ps=str[0]; fkind(cs,3,0,tc,ps); for(int i=0;i<4;i++)
cin>>a[i]; nkind(a,4,0,tn,pn); int i,j;
int ok=0;
int tot=0;
for(i=0;i<24;i++)
{
for(j=0;j<64;j++)
{
tot++;
double x=0.0;
char c[15];
equation(num[i],str[j],c);
x=ans2(c);
if(fabs(x-24.0)<=1e-5)
{
ok=1;
for(int k=0;k<11;k++)
cout<<c[k];
cout<<"=24"<<endl;
}
}
if(ok==1)
break;
if(tot==1536)
cout<<"NO ANSWER!\n";
}
return 0;
}
#include <iostream>
#include <cmath> using namespace std;
double ans2(char* c)
{
double a,b;
double sum;
char t;
a=c[2]-48;b=c[4]-48;
t=c[3];
// cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<endl;
if(t=='+')
{sum=a+b;}
if(t=='-')
{sum=a-b;}
if(t=='*')
{sum=a*b;}
if(t=='/')
{sum=a/b;}
//cout<<sum<<endl; a=sum;b=c[7]-48;
t=c[6];
// cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<endl;
if(t=='+')
{sum=a+b;}
if(t=='-')
{sum=a-b;}
if(t=='*')
{sum=a*b;}
if(t=='/')
{sum=a/b;} a=sum;b=c[10]-48;
t=c[9];
// cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<endl;
if(t=='+')
{sum=a+b;}
if(t=='-')
{sum=a-b;}
if(t=='*')
{sum=a*b;}
if(t=='/')
{sum=a/b;} return sum; } void fkind(char* a,int n,int i,char* t,char *p)
{
static int tot=0;
if(i==n)
{
for(int j=0;j<n;j++)
{
// cout<<t[j]<<" ";
p[tot++]=t[j];
}
// cout<<endl;
// cout<<endl<<tot++<<endl;
}
else if(i<n)
{
for(int j=0;j<4;j++)
{
t[i]=a[j];
fkind(a,n,i+1,t,p);
}
}
} void nkind(int* a,int n,int i,int* t,int *p)
{
static int tot=0;
if(i==n)
{
for(int j=0;j<n;j++)
{
// cout<<t[j]<<" ";
p[tot++]=t[j];
}
// cout<<endl;
// cout<<endl<<tot++<<endl;
}
else if(i<n)
{
for(int j=0;j<4;j++)
{
if(a[j]!=-1)
{
t[i]=a[j];
int s=a[j];
a[j]=-1;
nkind(a,n,i+1,t,p);
a[j]=s;
}
}
}
} void equation(int* a,char* b,char* c)
{
c[0]=c[1]='(';
c[5]=c[8]=')';
c[2]=a[0]+48;
c[4]=a[1]+48;
c[7]=a[2]+48;
c[10]=a[3]+48;
c[3]=b[0];
c[6]=b[1];
c[9]=b[2];
c[11]='\0';
} int main()
{
// char c[15];
int a[4];
int num[24][4];
int tn[4];
int* pn=num[0]; char str[64][3];
char cs[4]={'+','-','*','/'};
char tc[4];
char* ps=str[0]; fkind(cs,3,0,tc,ps); for(int i=0;i<4;i++)
cin>>a[i]; nkind(a,4,0,tn,pn); int i,j;
int ok=0;
int tot=0;
for(i=0;i<24;i++)
{
for(j=0;j<64;j++)
{
tot++;
double x=0.0;
char c[15];
equation(num[i],str[j],c);
x=ans2(c);
if(fabs(x-24.0)<=1e-5)
{
ok=1;
for(int k=0;k<11;k++)
cout<<c[k];
cout<<"=24"<<endl;
}
}
if(ok==1)
break;
if(tot==1536)
cout<<"NO ANSWER!\n";
}
return 0;
}