P1236 求助
查看原帖
P1236 求助
263594
寻逍遥2006楼主2021/4/17 11:54

蒟蒻求助!

#include <bits/stdc++.h>
using namespace std;
int a,b,c,d,ch1,ch2,ch3,step1,step2;
bool ac=false;
int js(int x,int y,int ch)
{
	switch(ch)
	{
		case 1:return x+y;
		case 2:return x-y;
		case 3:return x*y;
		case 4:return x/y;
	}
}
char show(int ch)
{
	switch(ch)
	{
		case 1:return '+';
		case 2:return '-';
		case 3:return '*';
		case 4:return '/';
	}
}
void tryed(int a,int b,int c,int d)
{
	for(ch1=1;ch1<=4&&!ac;ch1++)
	{
		step1=js(a,b,ch1);
		if(step1<0||(ch1==4&&a%b!=0)) continue;
		for(ch2=1;ch2<=4&&!ac;ch2++)
	    {
		    step2=js(step1,c,ch2);
		    if(step2<0||(ch2==4&&step1%c!=0)) continue;
		    for(ch3=1;ch3<=4&&!ac;ch3++)
	        {
		        if(24==js(step2,d,ch3)&&(ch3!=4||(ch3==4&&step2%d!=0)))
		        {
		        	cout<<a<<show(ch1)<<b<<'='<<step1<<endl;
		        	cout<<step1<<show(ch2)<<c<<'='<<step2<<endl;
		        	cout<<step2<<show(ch3)<<d<<'='<<js(step2,d,ch3);
		        	ac=true;
		        }
	        }
	    }
	}
	return;
}
int main()
{
	cin>>a>>b>>c>>d;
	tryed(a,b,c,d); if(ac) return 0;
	tryed(a,b,d,c); if(ac) return 0;
	tryed(a,c,b,d); if(ac) return 0;
	tryed(a,c,d,b); if(ac) return 0;
	tryed(a,d,b,c); if(ac) return 0;
	tryed(a,d,c,b); if(ac) return 0;
	tryed(b,a,c,d); if(ac) return 0;
	tryed(b,a,d,c); if(ac) return 0;
	tryed(b,c,a,d); if(ac) return 0;
	tryed(b,c,d,a); if(ac) return 0;
	tryed(b,d,a,c); if(ac) return 0;
	tryed(b,d,c,a); if(ac) return 0;
	tryed(c,a,b,d); if(ac) return 0;
	tryed(c,a,d,b); if(ac) return 0;
	tryed(c,b,a,d); if(ac) return 0;
	tryed(c,b,d,a); if(ac) return 0;
	tryed(c,d,a,b); if(ac) return 0;
	tryed(c,d,b,a); if(ac) return 0;
	tryed(d,a,b,c); if(ac) return 0;
	tryed(d,a,c,b); if(ac) return 0;
	tryed(d,b,a,c); if(ac) return 0;
	tryed(d,b,c,a); if(ac) return 0;
	tryed(d,c,a,b); if(ac) return 0;
	tryed(d,c,b,a); if(ac) return 0;
	cout<<"No answer!";   return 0;
}
2021/4/17 11:54
加载中...