Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

还以为是1A,原来只要过样例就能AC``

Posted by angeldust at 2012-08-12 15:07:36 on Problem 3983 and last updated at 2012-08-13 11:40:17
样例根本无法弄明白括号该如何打嘛,(除法也在打括号)。假设是每个等式必须都打2对括号(最后一次运算不打括号),这样的话就很简单了,不用考虑运算符优先级。
这样写上去居然1Y,膜拜自己的RP后看DISCUSS才知道只有一组测试数据,ORZ。

括号打法就5种(按我上面那样假设的话)直接暴力就行了,贴个暴力代码,不肯定是正确的,只是过了样例,话说这题果断该rejude啊。


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <set>
using namespace std;
char operators[4]={'+','-','*','/'};
int a[4],oper[3];
double calc(double a,int op,double b)
{
	switch (op)
	{
	case 0:return a+b;
	case 1:return a-b;
	case 2:return a*b;
	default:return a/(b!=0?b:0.00001);
	}
}
int main()
{
	for(int i=0;i<4;i++) scanf("%d",a+i);
	for(int i=0;i<64;i++)
	{
		int tmp=i;
		for(int j=0;j<3;j++,tmp>>=2) oper[j]=tmp&3;
		if (calc(calc(a[0],oper[0],a[1]),oper[1],calc(a[2],oper[2],a[3]))==24)
		{
			printf("(%d%c%d)%c(%d%c%d)\n",a[0],operators[oper[0]],a[1],operators[oper[1]],a[2],operators[oper[2]],a[3]);
			return 1;
		}
		if (calc(calc(calc(a[0],oper[0],a[1]),oper[1],a[2]),oper[2],a[3])==24)
		{
			printf("((%d%c%d)%c%d)%c%d\n",a[0],operators[oper[0]],a[1],operators[oper[1]],a[2],operators[oper[2]],a[3]);
			return 1;
		}
		if (calc(calc(a[0],oper[0],calc(a[1],oper[1],a[2])),oper[2],a[3])==24)
		{
			printf("(%d%c(%d%c%d))%c%d\n",a[0],operators[oper[0]],a[1],operators[oper[1]],a[2],operators[oper[2]],a[3]);
			return 1;
		}
		if (calc(a[0],oper[0],calc(calc(a[1],oper[1],a[2]),oper[2],a[3]))==24)
		{
			printf("%d%c((%d%c%d)%c%d)\n",a[0],operators[oper[0]],a[1],operators[oper[1]],a[2],operators[oper[2]],a[3]);
			return 1;
		}
		if (calc(a[0],oper[0],calc(a[1],oper[1],calc(a[2],oper[2],a[3])))==24)
		{
			printf("%d%c(%d%c(%d%c%d))\n",a[0],operators[oper[0]],a[1],operators[oper[1]],a[2],operators[oper[2]],a[3]);
			return 1;
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator