蓝桥杯——算式填符号

5. 算式填符号
【问题描述】

匪警请拨110,即使手机欠费也可拨通!
为了保障社会秩序,保护人民群众生命财产安全,警察叔叔需要与罪犯斗智斗勇,因而需要经常性地进行体力训练和智力训练!

某批警察叔叔正在进行智力训练:
1 2 3 4 5 6 7 8 9 = 110

请看上边的算式,为了使等式成立,需要在数字间填入加号或者减号(可以不填,但不能填入其它符号)。之间没有填入符号的数字组合成一个数,例如:12+34+56+7-8+9 就是一种合格的填法;123+4+5+67-89 是另一个可能的答案。

请你利用计算机的优势,帮助警察叔叔快速找到所有答案。
每个答案占一行。形如:
12+34+56+7-8+9
123+4+5+67-89

……


这道题最后也没有看明白官方java的解法,自己用了一个很蠢的暴力破解,也没有想出来合适的递归写法,要是有后来者恰巧写出了递归算法,希望可以评论在下方,感谢。

我的c++代码:


#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int loca(string x_long, string y)
{
	for(int i=0;i<x_long.length(); i++)
		if(x_long.substr(i,1)==y)
			return i+1;
}
int calc_(string x)
{
	int arr_num[9];//存数字
	int arr_sym[8];//存符号
	int count_num=0;//数字计数
	int count_sym=0;//符号计数 1 => +    0 => -
	int i=0,j=0;
	
	while(i<x.length())
	{
		if(x.substr(i,1)=="+")
		{
			string temp = x.substr(j,i-j);
			stringstream ss;
			ss<<temp;
			ss>>arr_num[count_num++];
			arr_sym[count_sym++]=1;
			j=i+1;
		}
		if(x.substr(i,1)=="-")
		{
			string temp = x.substr(j,i-j);
			stringstream ss;
			ss<<temp;
			ss>>arr_num[count_num++];
			arr_sym[count_sym++]=0;
			j=i+1;
		}
		i++;
	}
	string temp = x.substr(j,i-j);
	stringstream ss;
	ss<<temp;
	ss>>arr_num[count_num++];

	int ans=arr_num[0];
	for(int k=1; k<count_num;k++)
	{
		if(arr_sym[k-1]==1)
			ans+=arr_num[k];
		if(arr_sym[k-1]==0)
			ans-=arr_num[k];
	}
	return ans;
}
int main()
{	
	int answer=0;
	for(int a1=0;a1<3;a1++)
	{
		string x1="123456789";
		if(a1==0)
			x1.insert(loca(x1,"1"),"+");
		if(a1==1)
			x1.insert(loca(x1,"1"),"-");

		for(int a2=0;a2<3;a2++)
		{
			string x2=x1;
			if(a2==0)
				x2.insert(loca(x2,"2"),"+");
			if(a2==1)
				x2.insert(loca(x2,"2"),"-");

			for(int a3=0;a3<3;a3++)
			{
				string x3= x2; 
				if(a3==0)
					x3.insert(loca(x3,"3"),"+");
				if(a3==1)
					x3.insert(loca(x3,"3"),"-");
				
				for(int a4=0;a4<3;a4++)
				{
					string x4= x3; 
					if(a4==0)
						x4.insert(loca(x4,"4"),"+");
					if(a4==1)
						x4.insert(loca(x4,"4"),"-");

					for(int a5=0;a5<3;a5++)
					{
						string x5= x4; 
						if(a5==0)
							x5.insert(loca(x5,"5"),"+");
						if(a5==1)
							x5.insert(loca(x5,"5"),"-");
			
						for(int a6=0;a6<3;a6++)
						{
							string x6= x5; 
							if(a6==0)
								x6.insert(loca(x6,"6"),"+");
							if(a6==1)
								x6.insert(loca(x6,"6"),"-");
				
							for(int a7=0;a7<3;a7++)
							{
								string x7= x6; 
								if(a7==0)
									x7.insert(loca(x7,"7"),"+");
								if(a7==1)
									x7.insert(loca(x7,"7"),"-");
								
								for(int a8=0;a8<3;a8++)
								{
									string x8= x7; 
									if(a8==0)
										x8.insert(loca(x8,"8"),"+");
									if(a8==1)
										x8.insert(loca(x8,"8"),"-");

									int ans=calc_(x8);
									if(ans==110)
									{
										cout<<x8<<endl;
										answer++;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	cout<<answer<<endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值