比赛 |
20100914 |
评测结果 |
AAAWAWAWWA |
题目名称 |
算24点 |
最终得分 |
60 |
用户昵称 |
wangwangdog |
运行时间 |
0.000 s |
代码语言 |
C |
内存使用 |
0.00 MiB |
提交时间 |
2010-09-14 22:33:15 |
显示代码纯文本
#include<stdio.h>
int a[5],number[5],b[5],num[5],j[5],k,finished,zheng,i,l,an[4];
FILE *fin,*fout;
int make(int y)
{
if(zheng==0)return -1;
else
{
int x=j[y],u=y;
if(x==1)return number[u]+number[u+1];
if(x==2)
{
if(number[u]-number[u+1]<=0)zheng=0;
return number[u]-number[u+1];
}
if(x==3)return number[u]*number[u+1];
if(x==4)
{
if(number[u]%number[u+1]!=0)zheng=0;
return number[u]/number[u+1];
}
}
}
void jisuan()
{
int o;
zheng=1;
for(o=1;o<=4;o++)
number[o]=a[o];
for(o=0;o<=5;o++)
num[o]=0;
int tt=b[1];
num[tt]=1;num[tt+1]=1;
int all=0;
all=make(b[1]);
for(o=1;o<=4;o++)
if(num[o]==1)number[o]=all;
tt=b[2];
if(num[tt]!=0&&num[tt]!=2)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt])num[o]=2;
}
num[tt]=2;
if(num[tt+1]!=0&&num[tt+1]!=2)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt+1])num[o]=2;
}
num[tt+1]=2;
all=make(b[2]);
for(o=1;o<=4;o++)
if(num[o]==2)number[o]=all;
tt=b[3];
if(num[tt]!=0&&num[tt]!=3)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt])num[o]=3;
}
num[tt]=3;
if(num[tt+1]!=0&&num[tt+1]!=3)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt+1])num[o]=3;
}
num[tt+1]=3;
all=make(b[3]);
for(o=1;o<=4;o++)
if(num[o]==3)number[o]=all;
if(number[b[3]]==24&&zheng==1)
{
for(o=1;o<=3;o++)
an[o]=j[o];
finished=1;
}
}
int main()
{
fin=fopen("point24.in","rb");
fout=fopen("point24.out","wb");
finished=0;
for(i=1;i<=4;i++)
fscanf(fin,"%d",&a[i]);
for(j[1]=1;j[1]<=4;j[1]++)
for(j[2]=1;j[2]<=4;j[2]++)
for(j[3]=1;j[3]<=4;j[3]++)
{
int t=1;
if(finished!=1)
{
for(k=1;k<=3;k++)
b[k]=0;
}
while(b[1]<=3&&finished!=1)
{
b[t]++;
if(b[t]>3){if(t!=1)b[t]=0;t--;}
else
{
int flag=1;
for(l=1;l<=t-1;l++)
if(b[l]==b[t])flag=0;
if(flag==1)
{
if(t==3)jisuan();
else{t++;}
}
}
}
}
if(finished!=1)fprintf(fout,"No answer!");
else
{
for(i=1;i<=3;i++)
j[i]=an[i];
int o;
zheng=1;
for(o=1;o<=4;o++)
number[o]=a[o];
for(o=0;o<=5;o++)
num[o]=0;
int tt=b[1];
int aa,bb;
aa=number[tt],bb=number[tt+1];
if(aa<bb){int cc=aa;aa=bb;bb=cc;}
fprintf(fout,"%d",aa);
if(j[tt]==1)fprintf(fout,"+%d=",bb);
if(j[tt]==2)fprintf(fout,"-%d=",bb);
if(j[tt]==3)fprintf(fout,"*%d=",bb);
if(j[tt]==4)fprintf(fout,"/%d=",bb);
num[tt]=1;num[tt+1]=1;
int all=0;
all=make(b[1]);
for(o=1;o<=4;o++)
if(num[o]==1)number[o]=all;
fprintf(fout,"%d\r\n",all);
tt=b[2];
aa=number[tt],bb=number[tt+1];
if(aa<bb){int cc=aa;aa=bb;bb=cc;}
fprintf(fout,"%d",aa);
if(j[tt]==1)fprintf(fout,"+%d=",bb);
if(j[tt]==2)fprintf(fout,"-%d=",bb);
if(j[tt]==3)fprintf(fout,"*%d=",bb);
if(j[tt]==4)fprintf(fout,"/%d=",bb);
if(num[tt]!=0&&num[tt]!=2)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt])num[o]=2;
}
num[tt]=2;
if(num[tt+1]!=0&&num[tt+1]!=2)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt+1])num[o]=2;
}
num[tt+1]=2;
all=make(b[2]);
for(o=1;o<=4;o++)
if(num[o]==2)number[o]=all;
fprintf(fout,"%d\r\n",all);
tt=b[3];
aa=number[tt],bb=number[tt+1];
if(aa<bb){int cc=aa;aa=bb;bb=cc;}
fprintf(fout,"%d",aa);
if(j[tt]==1)fprintf(fout,"+%d=",bb);
if(j[tt]==2)fprintf(fout,"-%d=",bb);
if(j[tt]==3)fprintf(fout,"*%d=",bb);
if(j[tt]==4)fprintf(fout,"/%d=",bb);
if(num[tt]!=0&&num[tt]!=3)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt])num[o]=3;
}
num[tt]=3;
if(num[tt+1]!=0&&num[tt+1]!=3)
{
for(o=1;o<=4;o++)
if(num[o]==num[tt+1])num[o]=3;
}
num[tt+1]=3;
all=make(b[3]);
fprintf(fout,"%d",all);
}
fclose(fin);
fclose(fout);
return 0;
}