这是一道神奇的题,神奇的不可描述
ac
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
double a[4];
bool flag;
double cnt(double a,double b,int k)
{
double s=0.0;
switch(k)
{
case 0:s=a+b;break;
case 1:s=a-b;break;
case 2:s=a*b;break;
case 3:if(b!=0)s=a/b;break;
case 4:s=b-a;break;
case 5:if(a!=0)s=b/a;break;
}
return s;
}
bool judge(int i,int j,int k)
{
if(cnt(cnt(cnt(a[0],a[1],i),a[2],j),a[3],k)==24) return true;
if(cnt(cnt(a[0],a[1],i),cnt(a[2],a[3],k),j)==24) return true;
return false;
}
void solve()
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
for(int k=0;k<6;k++)
{
if(judge(i,j,k))
{
flag=true;
return;
}
}
}
}
}
int main()
{
int N;
cin>>N;
while(N--)
{
flag=false;
for(int i=0;i<4;i++) cin>>a[i];
sort(a,a+4);
do
{
solve();
if(flag) break;
}
while(next_permutation(a,a+4));
if(flag)printf("Yes\n");
else printf("No\n");
}
}
未ac 求大神带
测试数据过了。。神奇
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
double used[5],number[5];
double nownumber[4]={0};
int ops[4]={0},flag=0;
char opType[7]={' ','+','-','*','/','@','#'};
void calcTypel()
{
float a=nownumber[0];
for(int i=1;i<=3;i++)
{
switch(opType[ops[i-1]])
{
case '+':a+=nownumber[i];
break;
case '-':a-=nownumber[i];
break;
case '*':a*=nownumber[i];
break;
case '/':
if(nownumber[i]==0)return ;
a/=nownumber[i];
break;
case '@':
a=nownumber[i]-a;
break;
case '#':
if(a==0)return ;
a=nownumber[i]/a;
break;
}
}
if(a==24.0)flag=1;
}
void makeOperation(int depth)
{
if(depth>=3)
{
calcTypel();
return ;
}
for(int i=1;i<=6;i++)
{
ops[depth]=i;
makeOperation(depth+1);
}
}
void makeNumber()
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(j!=i)
for(int k=1;k<=4;k++)
{
if(k!=j&&k!=i)
for(int p=1;p<=4;p++)
{
if(p!=j&&p!=k&&p!=i)
{
nownumber[0]=number[i];
nownumber[1]=number[j];
nownumber[2]=number[k];
nownumber[3]=number[p];
makeOperation(0);
}
}
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=1;i<=4;i++)
scanf("%lf",&number[i]);
memset(used,0,sizeof(used));
memset(ops,0,sizeof(ops));
memset(nownumber,0,sizeof(nownumber));
flag=0;
makeNumber();
if(flag==1)printf("YES\n");
else printf("NO\n");}
return 0;
}