记录编号 348491 评测结果 AAAAAAAAAA
题目名称 算24点 最终得分 100
用户昵称 GravatarZWOI_ヤシニャ 是否通过 通过
代码语言 C 运行时间 0.002 s
提交时间 2016-11-14 11:46:14 内存使用 0.29 MiB
显示代码纯文本
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int b[4][4],book[10],q;
char e[5];
int a[10];
void dfs(int x,int y)
{
	int i,j,t,p;
	if(y==4)
	{
		if(b[3][3]==24)
		{
			for(t=1;t<=3;t++)
				printf("%d%c%d=%d\n",b[t][1],e[t],b[t][2],b[t][3]);
			exit(0);
		}
		return ;
	}
	for(j=1;j<=4;j++)
	{
		if(!book[j])
		{
			if(a[x]<a[j])
			{
				b[y][1]=a[j];
				b[y][2]=a[x];
			}
			else
			{
				b[y][1]=a[x];
			    b[y][2]=a[j];
			}
			p=5;
			while(a[p])
				p++;
			book[p]=book[j]=1;
			for(i=1;i<=4;i++)
			{
				if(i==1)
				{
					b[y][3]=a[x]+a[j];
					e[y]='+';
					a[p]=b[y][3];
					dfs(p,y+1);
				}	
				if(i==2)
				{
					b[y][3]=b[y][1]-b[y][2];
					e[y]='-';
					a[p]=b[y][3];
					dfs(p,y+1);
				}
				if(i==3)
				{
					b[y][3]=a[x]*a[j];
					e[y]='*';
					a[p]=b[y][3];
					dfs(p,y+1);
				}
				if(i==4&&(b[y][2]&&b[y][1]%b[y][2]==0))
				{
					b[y][3]=a[j]/a[x];
					e[y]='/';
					a[p]=b[y][3];
					dfs(p,y+1);
				}
			}
			book[p]=a[p]=book[j]=0;
		}
	}
	
}
int main()
{
	int i;
	freopen("point24.in","r",stdin);
	freopen("point24.out","w",stdout);
	scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]);
	for(i=1;i<=4;i++)
	{
		book[i]=1;
		memset(b,0,sizeof(b));
		dfs(i,1);
		book[i]=0;
	}
	if(!q)
		printf("No answer!");
	return 0;
}