记录编号 |
2409 |
评测结果 |
WAAAAWAAAA |
题目名称 |
算24点 |
最终得分 |
80 |
用户昵称 |
zqzas |
是否通过 |
未通过 |
代码语言 |
C |
运行时间 |
0.010 s |
提交时间 |
2008-09-20 14:31:58 |
内存使用 |
1.52 MiB |
显示代码纯文本
#include<stdio.h>
#include<stdlib.h>
int num[4],taken[4],record[5][3];
FILE *ifp,*ofp;
void ini(void);
void next(long sum,long k);
void swap(int *a,int *b);
int main(void)
{
long i;
ini();
for(i=0;i<4;++i)
{
taken[i]=1;
next(num[i],2);
taken[i]=0;
}
fprintf(ofp,"No answer!");
fclose(ifp);
fclose(ofp);
return 0;
}
void ini(void)
{
ifp=fopen("point24.in","r");
ofp=fopen("point24.out","w");
fscanf(ifp,"%ld%ld%ld%ld",&num[0],&num[1],&num[2],&num[3]);
}
void next(long sum,long k)
{
long i,j;
if(k==5)
{
if(sum==24)
{
for(i=2;i<5;++i)
{
if(record[i][0]<record[i][1]
&& record[i][2]!=1 && record[i][2]!=3)
swap(&record[i][0],&record[i][1]);
fprintf(ofp," %ld",record[i][0]);
switch (record[i][2])
{
case 0:
fprintf(ofp,"+%ld=%ld",record[i][1],
record[i][0]+record[i][1]);
break;
case 1:
fprintf(ofp,"-%ld=%ld",record[i][1],
record[i][0]-record[i][1]);
break;
case 2:
fprintf(ofp,"*%ld=%ld",record[i][1],
record[i][0]*record[i][1]);
break;
case 3:
fprintf(ofp,"/%ld=%ld",record[i][1],
record[i][0]/record[i][1]);
}
if(i!=4)
fputc('\n',ofp);
}
fclose(ifp);
fclose(ofp);
exit(0);
}
return;
}
for(i=0;i<4;++i)
if(!taken[i])
{
taken[i]=1;
record[k][0]=sum;
record[k][1]=num[i];
for(j=0;j<4;++j)
{
record[k][2]=j;
switch(j)
{
case 0:
next(record[k][0]+record[k][1],k+1);
break;
case 1:
if(record[k][0]-record[k][1]>0)
next(record[k][0]-record[k][1],k+1);
swap(&record[k][0],&record[k][1]);
if(record[k][0]-record[k][1]>0)
next(record[k][0]-record[k][1],k+1);
break;
case 2:
next(record[k][0]*record[k][1],k+1);
break;
case 3:
if(record[k][1]!=0 && record[k][0]%record[k][1]==0)
next(record[k][0]/record[k][1],k+1);
swap(&record[k][0],&record[k][1]);
if(record[k][1]!=0 && record[k][0]%record[k][1]==0)
next(record[k][0]/record[k][1],k+1);
break;
}
record[k][2]=0;
}
taken[i]=0;
record[k][0]=record[k][1]=0;
}
}
void swap(int *a,int *b)
{
int c;
c=(*a);
(*a)=(*b);
(*b)=c;
}