24点


1.全排列(通过加括号让它从左到右运算)

2.除法分顺序

3.加括号

      a.左邻括号的运算符为“/”,则括号必须保留,即…/(s1 op s2)…形式。左邻括号的预算符为“*”或“-”。

      b.而op为“+”或“-”,则保留括号,即…*(s1+s2)…或…-(s1+s2)…或…*(s1-s2)…或…-(s1-s2)…。

      c.右邻括号的运算符为“*”或“/”,而op为“+”或“-”,原式中的op运算必须优先进行,因此括号不去除,即(s1+s2)*…除上述情况外,可以括号去除.。

 

 

#include<iostream>
#include<string>
#include<set>
#include<strstream>
using namespace std;

typedef struct Node
{
	char notation;
	int Numerator, Denominator;
	string expression;
}NODE ;

NODE Num[4];
set<string> FinalStrings;
int count;

NODE operator + ( const NODE x, const NODE y  )
{
	NODE temp;
	temp.notation = '+';
	temp.Numerator = x.Numerator * y.Denominator + y.Numerator * x.Denominator;
	temp.Denominator = x.Denominator * y.Denominator;
	temp.expression = x.expression + " + " + y.expression;
	return temp;
}
NODE operator - ( const NODE x, const NODE y  )
{
	NODE temp;
	temp.notation = '-';
	temp.Numerator = x.Numerator * y.Denominator - y.Numerator * x.Denominator;
	temp.Denominator = x.Denominator * y.Denominator;
	if( y.notation == '+' || y.notation == '-' )
		temp.expression =  x.expression + " - " + '('  + y.expression + ')';
	else
		temp.expression = x.expression + " - " + y.expression;
	return temp;
}
NODE operator * ( const NODE x, const NODE y  )
{
	NODE temp;
	temp.notation = '*';
	temp.Numerator = x.Numerator * y.Numerator;
	temp.Denominator = x.Denominator * y.Denominator;
	if( x.notation == '+' || x.notation == '-' )
		temp.expression = '(' + x.expression + ')' + " x " + y.expression;
	else if(y.notation == '+' || y.notation == '-' )
		temp.expression = x.expression + " x " + '(' + y.expression + ')';
	else if( x.notation == '+' || x.notation == '-' && y.notation == '+' || y.notation == '-' )
		temp.expression = '(' + x.expression + ')' + " x " + '(' + y.expression + ')';
	else
		temp.expression = x.expression + " x " + y.expression;
	return temp;
}
NODE operator / ( const NODE x, const NODE y  )
{
	NODE temp;
	temp.notation = '/';
	temp.Numerator = x.Numerator * y.Denominator;
	temp.Denominator =  y.Numerator * x.Denominator;
	if( x.notation == '+' || x.notation == '-'  )
		temp.expression = '(' + x.expression + ')' + " / " + y.expression;
	else if( y.notation )
		temp.expression = x.expression + " / " + '(' + y.expression + ')';
	else
		temp.expression = x.expression + " / " + y.expression;
	return temp;
}
void copy( NODE *x, const NODE y )
{
	x -> expression = y.expression;
	x -> Denominator = y. Denominator;
	x -> Numerator = y.Numerator;
	x -> notation = y.notation;
}
void Match( int n )
{
	if( n == 1 )
	{
		if(  Num[0].Numerator / Num[0].Denominator == 24 && !(Num[0].Numerator % Num[0].Denominator) && FinalStrings.find( Num[0].expression )==FinalStrings.end() )
			{
				FinalStrings.insert( Num[0].expression );
				cout << Num[0].expression << endl;
				count ++;
			}
		return ;
	}
	for( int i = 0; i < n; i++ )
		for( int j = i + 1; j < n; j++ )
		{
			NODE x, y;
			copy( &x, Num[i] );copy( &y, Num[j] );
			if( j != n - 1 ) copy( &Num[j], Num[n-1]);

			copy( & Num[i], x + y ); Match( n - 1 );
			copy( & Num[i], x - y ); Match( n - 1 );
			copy( & Num[i], y - x ); Match( n - 1 );
			copy( & Num[i], x * y ); Match( n - 1 );
			if(y.Numerator)copy( & Num[i], x / y ); Match( n - 1 );
			if(x.Numerator)copy( & Num[i], y / x ); Match( n - 1 );

			copy(&Num[i] , x);copy(& Num[j] , y);
		}
}
int main()
{
	strstream part;
	while(1)
	{
		cout << "Please enter the four integers separated by a space: " << endl;
		for( int i = 0; i < 4; i++ )
		{
			cin >> Num[i].Numerator;
			Num[i].Denominator = 1;
			Num[i].notation = NULL;
			part << Num[i].Numerator;
			part >> Num[i].expression;
			part.clear();
		}
		count = 0;
		Match( 4 );
		if( !count )
			cout << "No answer !" << endl;
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值