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

我这样AC,悲剧了吗?

Posted by wcfairytale at 2011-10-09 02:53:32 on Problem 3983
#include<stdio.h>
#include<string.h>

const char mode[11][15] ={
	"1+1+1+1#",
	"(1+1)+1+1#",
	"1+(1+1)+1#",
	"1+1+(1+1)#",
	"(1+1+1)+1#",
	"1+(1+1+1)#",
	"((1+1)+1)+1#",
	"(1+(1+1))+1#",
	"1+((1+1)+1)#",
	"1+(1+(1+1))#",
	"(1+1)+(1+1)#"
};
char cal[4] ={
	'+',
	'-',
	'*',
	'/'
};

double num[4];
char symbol[11];
int num_head;
int num_tail;
int symbol_head;
int symbol_tail;

char tc;
double left,right;


bool is_bigger(char a,char b)
{
	if('#' == a)
		return false;
	if('#' == b)
		return true;
	if('(' == a || '(' == b)
		return true;
	if('+' == a || '-' == a || ')' == a)
		return false;
	if('*' == a || '/' == a)
	{
		if('+' == b || '-' == b )
			return true;
		else 
			return false;
	}
}
double do_cal(double left,double right, char tc)
{
	if('+' == tc)
		return left+right;
	else if('-' == tc)
		return left-right;
	else if('*' == tc)
		return left*right;
	else
		return left/right;
}

double do_24(char* s)
{
	int i;
	for(i=0; i < strlen(s); i++)
	{
		if('1' <= s[i] && s[i] <= '9')
			num[++num_tail] = s[i]-'0';
		else if ('0' == s[i])
			num[++num_tail] = 10.0;
		else
			if( is_bigger(s[i], symbol[symbol_tail]) )
				symbol[++symbol_tail] = s[i];
			else
			{
				if( ')' == s[i] || '#' == s[i])
				{
					tc = symbol[symbol_tail--];
					symbol_tail--;//'('out
					right = num[num_tail--];
					left = num[num_tail--];
					num[++num_tail] = do_cal(left,right,tc);
				}
				else 
				{
					tc = symbol[symbol_tail--];
					right = num[num_tail--];
					left = num[num_tail--];
					num[++num_tail] = do_cal(left,right,tc);
					i--;//normal cal
				}
			}
	}

	return num[num_tail];
}

void print(char *s)
{
	int i;
	for(i=0;i<strlen(s);i++)
	{
		if(s[i]=='0')printf("10");
		else if(s[i]=='#');
		else printf("%c",s[i]);
	}
	printf("\n");
}
int main()
{	
	int i,j,c[3],cn,nn;
	char s[15];
	double re;

	int a[4];
	scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]);

	for(i=0;i<4;i++)
		if(10 == a[i])a[i]=0;

	for(i=0;i<11;i++)
	{
		//strcpy(s,mode[i]);

		for(c[0]=0;c[0]<4;c[0]++)
			for(c[1]=0;c[1]<4;c[1]++)
				for(c[2]=0;c[2]<4;c[2]++)
				{
					num_head		=	-1;
					num_tail		=	-1;
					symbol_head		=	0;
					symbol_tail		=	0;
					symbol[0] = '#';
					cn = 0;
					nn = 0;

					strcpy(s,mode[i]);

					for(j=0;j<strlen(s);j++)
					{
						if('+' == s[j])//cal
							s[j] = cal[c[cn++]];
						else if('(' == s[j] || ')' == s[j]) //brackets
							;
						else if( '#' == s[j]) //special symbol
							;
						else
							s[j] = a[nn++] + '0';
					}

					re = do_24(s);

					if( -1e-6 <= re-24 && re-24 <= 1e-6)
					{
						
						print(s);
						return 0;
					}
				}
	}			
	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