Pro
题目大概长这样
不过我没完全按照题目的答案输出 而是直接输出组成24的表达式
Sol
代码写完之后就长这样 注释不想删了留作纪念(逃
其实就是搜索+栈求中缀表达式
思路比较简单 可能实现起来稍微有点麻烦吧
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define L 100005
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define _ceil(_,__) (_+(__-1))/__
#define debug(_) cout<<endl<<"d::"<<_<<endl<<endl
#define type(_) typeid(_).name()
inline LL read() {
LL x = 0, f = 1;char c = getchar();
while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
return x * f;
}
struct Node {
LL ans;
bool get;
Node(LL aa , bool gg) {
ans=aa , get=gg;
}
};
LL a[10];
LL temp1[10] , temp3[10];
char temp2[10];
char ch[4]={'+','-','*','/'};
map<char , LL>yx;
map<string , LL>vis;
void init() {
yx['+'] = yx['-'] = 0;
yx['*'] = yx['/'] = 1;
yx['('] = -1;
yx[')'] = -1;
}
Node mul(LL x , LL y , char c) {
if(c=='+') return Node(x+y,true);
if(c=='-') return Node(x-y,true);
if(c=='*') return Node(x*y,true);
if(c=='/') {
if(y==0)
return Node(INF,false);
if(x%y==0)
return Node(x/y,true);
return Node(INF,false);
}
}
Node cal(string s) {
stack<int>s1;
stack<char>s2;
LL len = s.length()-1;
Fo(i,0,len) {
if(s[i]>='0'&&s[i]<='9') {
if(s[i+1]>='0'&&s[i+1]<='9') {
s1.push((s[i]-'0')*10+(s[i+1]-'0'));
i++;
} else {
s1.push(s[i]-'0');
}
}
else {
if(s[i]=='(') {
s2.push(s[i]);
continue;
}
if(s2.empty()) {
s2.push(s[i]);
continue;
}
if(yx[s[i]]<=yx[s2.top()]) {
if(s[i]==')') {
while(1) {
LL t1 , t2;
if(s2.top()=='(') {
s2.pop();
break;
}
t1=s1.top(); s1.pop(); t2=s1.top(); s1.pop();
Node temp = mul(t2 , t1 , s2.top());
if(temp.get==false)
return Node(INF , false);
s1.push(temp.ans);
s2.pop();
}
} else {
LL t1 , t2;
t1=s1.top(); s1.pop(); t2=s1.top(); s1.pop();
Node temp = mul(t2 , t1 , s2.top());
if(temp.get==false)
return Node(INF , false);
s1.push(temp.ans);
s2.pop();
s2.push(s[i]);
}
} else {
s2.push(s[i]);
}
}
}
while(!s2.empty()) {
LL t1 , t2;
t1=s1.top(); s1.pop(); t2=s1.top(); s1.pop();
Node temp = mul(t2 , t1 , s2.top());
if(temp.get==false)
return Node(INF , false);
s1.push(temp.ans);
s2.pop();
}
return Node(s1.top(),true);
}
void dfs(LL flag , LL num[] , LL nn , char cr[] , LL cc , LL vis1[]) {
if(nn+cc==7) {
Fo(i,1,3)
Fo(j,i+1,4) {
string temp;
if(i==1)
temp = "(";
else
temp = "";
LL ff = 1 , cc = 0 , rr = 0;
while(1) {
if(ff==1) {
cc++;
if(num[cc]>=10) {
temp+=(num[cc]/10+'0');
temp+=(num[cc]%10+'0');
} else
temp+=(num[cc]+'0');
ff = 2;
} else {
if(cc==j)
temp+=')';
rr++;
temp+=cr[rr];
if(cc==i-1)
temp+='(';
ff = 1;
}
if(cc+rr==7)
break;
}
if(j==4)
temp+=')';
Node tt = cal(temp);
if(tt.get==false)
return ;
if(tt.ans==24)
if(vis[temp]!=1) {
vis[temp] = 1;
cout<<temp<<endl;
}
// cout<<temp<<" "<<cal(temp).ans<<endl;
}
// cout<<num[1]<<cr[1]<<num[2]<<cr[2]<<num[3]<<cr[3]<<num[4]<<endl;
return ;
}
if(flag==1) {
Fo(i,1,4) {
if(vis1[i]) continue;
num[nn+1] = a[i];
vis1[i] = 1;
dfs(2 , num , nn+1 , cr , cc , vis1);
vis1[i] = 0;
}
}
if(flag==2) {
Fo(i,0,3) {
cr[cc+1] = ch[i];
dfs(1 , num , nn , cr , cc+1 , vis1);
}
}
}
int main() {
freopen("data.txt","r",stdin);
init();
/*string xx;
cin>>xx;
cout<<cal(xx).ans;*/
while(scanf("%lld%lld%lld%lld",&a[1],&a[2],&a[3],&a[4])) {
if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0)
break;
Ms(temp3 , 0);
dfs(1 , temp1 , 0 , temp2 , 0 , temp3);
}
return 0;
}