Problem A. Seventeen

Problem A. Seventeen

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes

Seventeen is a game where players try to construct an equation that evaluates to 17 using operators ±*
and parentheses () and using each of the given numbers exactly once.
Construct an equation using integers from 1 to n exactly once with the value of 17.

Input

The input contains one integer n (1 ≤ n ≤ 50).

Output

If there is no solution, output −1. Otherwise, output an expression consisting of +, -, *, (, and ). The
length of the expression should not exceed 1000 characters.

Example

standard input
10

standard output

1+2+3+4+5+6+7+8-9-10

题目大概意思也就是判断从1通过+、-、*到n能不能凑到17

用了非常暴力的方式:打表(yyds)

#include<iostream>
using namespace std;
string arr[55];
int main()
{
	int n;
	cin>>n;
	if(n>=1 && n<=3){
		cout<<-1;
		return 0;
	}
	arr[4]="(2+4)*3-1";
	arr[5]="3*5+4-(1*2)";
	arr[6]="1-2+3+4+5+6";
	arr[7]="(1+2)*3+4+5+6-7";
	arr[8]="(1+2+3-4)*5+6-7+8";
	arr[9]="1+2+3+4+5-6+7-8+9";
	arr[10]="1+2+3+4+5+6+7+8-9-10";
	arr[11]="(1+2+3+4-5)*6+7-8+9-10-11";
	arr[12]="(1+2+3+4-5)*6-7-8-9+10-11+12";
	arr[13]="1+2+3+4+5+6-7+8-9-10-11+12+13";
	arr[14]="1+2+3+4+5+6+7+8-9-10+11-12-13+14";
	arr[15]="(1+2+3+4+5-6+7-8)*9+10-11-12-13-14-15";
	arr[16]="(1+2+3+4+5+6+7+8+9-10-11-12-13)*14+15+16";
	arr[17]="1+2+3+4+5+6+7+8+9+10-11-12+13-14-15-16+17";
	arr[18]="1+2+3+4+5+6+7+8+9+10+11+12-13-14-15+16-17-18";
	arr[19]="((1+2+3+4+5+6+7+8+9-10-11-12-13)*14+15-16+17)*18-19";
	arr[20]="(1+2+3+4+5+6+7+8+9+10-11-12+13-14-15-16)*17+18+19-20";
	arr[21]="1+2+3+4+5+6+7+8+9+10+11+12+13+14-15-16-17-18+19-20-21";
	arr[22]="1+2+3+4+5+6+7+8+9+10+11+12-13+14-15-16-17-18-19-20+21+22";
	arr[23]="(1+2+3+4+5+6+7+8+9+10+11-12-13-14-15-16)*17+18-19+20+21+22+23";
	arr[24]="(1+2+3+4+5+6+7+8+9+10+11+12+13-14+15-16-17-18-19-20)*21+22-23-24";
	arr[25]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18-19-20-21-22-23-24-25";
	arr[26]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16-17-18-19-20-21+22-23-24-25+26";
	arr[27]="(1+2+3+4+5+6+7+8+9+10+11+12+13-14+15-16-17-18-19-20-21+22)*23-24+25-26-27";
	arr[28]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14-15-16-17-18-19-20)*21+22+23+24-25-26+27-28";
	arr[29]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18-19-20-21-22-23-24-25+26-27-28+29";
	arr[30]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19-20+21-22-23-24-25-26-27-28-29+30";
	arr[31]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17-18-19-20-21-22-23-24+25-26)*27-28-29-30-31";
	arr[32]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15-16-17+18-19-20-21-22-23-24+25)*26+27+28+29-30-31-32";
	arr[33]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21-22-23-24+25-26-27-28-29-30-31-32+33";
	arr[34]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23-24-25-26-27-28-29+30-31-32-33-34";
	arr[35]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17-18-19+20-21-22-23-24-25-26-27+28)*29+30-31+32+33+34+35";
	arr[36]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19-20-21-22-23-24-25-26-27-28+29)*30+31-32+33-34-35-36";
	arr[37]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25-26-27-28-29-30-31-32-33-34+35-36-37";
	arr[38]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26-27+28-29-30-31-32-33-34-35-36-37-38";
	arr[39]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21-22-23-24-25-26-27-28-29-30+31-32)*33+34-35+36+37+38+39";
	arr[40]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22-23-24-25-26-27-28-29+30-31-32-33)*34-35+36-37-38-39-40";
	arr[41]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28-29-30-31-32+33-34-35-36-37-38-39-40-41";
	arr[42]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27-28-29-30-31-32-33-34-35-36-37-38-39+40-41+42";
	arr[43]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24-25-26-27-28-29-30+31-32-33-34-35-36)*37+38-39+40+41+42+43";
	arr[44]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25-26-27-28-29+30-31-32-33-34-35-36-37)*38-39-40-41-42-43-44";
	arr[45]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28-29-30+31-32-33-34-35-36-37-38-39-40-41-42-43+44+45";
	arr[46]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30-31-32-33-34-35-36-37+38-39-40-41-42-43-44-45+46";
	arr[47]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27-28-29+30-31-32-33-34-35-36-37-38-39-40)*41+42-43+44+45+46+47";
	arr[48]="(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27-28-29-30-31-32-33-34+35-36-37-38-39-40)*41-42+43-44-45-46-47-48";
	arr[49]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32-33-34-35-36-37-38-39-40-41-42-43+44-45-46-47-48+49";
	arr[50]="1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33-34+35-36-37-38-39-40-41-42-43-44-45-46-47-48-49+50";
	cout<<arr[n];
	return 0;
}

这是打表的代码,用的递归,跑了好久

特别要注意的就是

1、如果遇到了*一定要记得加括号,把从头到乘全部都括起来。

2、要开long long 如果不开long long 会出bug。

#include<iostream>
using namespace std;
char arr[55];
bool vis[55];
long long num=0;
void panduan(long long zhi,long long zuida,long long dadian){
	if(vis[zuida]) return;
	if(zuida<dadian){
		if(zhi==17){
			vis[zuida]=true;
			for(long long i=1;i<=zuida;i++){
				if(arr[i]=='*'){
					cout<<"(";
				}
			}
			for(long long i=1;i<=zuida;i++){
				if(arr[i]=='*'){
					cout<<")";
				}
				cout<<arr[i]<<i;
			}
		}
		return;
	}else{
		arr[dadian]='+';
		panduan(zhi+dadian,zuida,dadian+1);
		arr[dadian]='-';
		panduan(zhi-dadian,zuida,dadian+1);
		arr[dadian]='*';
		panduan(zhi*dadian,zuida,dadian+1);
	}
}
int main()
{
	for(long long i=1;i<=50;i++){
		num=0;
		cout<<"arr["<<i<<"]=\"";
		panduan(1,i,2);
		cout<<"\";";
		cout<<endl;
	}
	//for(long long i=1;i<=50;i++){
	//	if(!vis[i]) cout<<i<<" ";
	//}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值