玄学 WA 80pts,疑似 SPJ 的问题
  • 板块P1236 算24点
  • 楼主array2022
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/2 21:30
  • 上次更新2025/2/2 21:33:23
查看原帖
玄学 WA 80pts,疑似 SPJ 的问题
690315
array2022楼主2025/2/2 21:30

下载测试数据自测发现都没问题,可能是我读题读错了,欢迎找错。(列文虎克

思路:枚举所有的后缀表达式。

#include<bits/stdc++.h>
#define pii pair<int,int> 
#define mp(a,b) make_pair(a,b)
#define rep(x) for(int x=0;x<4;x++)
#define st first
#define nd second
using namespace std;
pii a[8];
char b[4]={'+','-','*','/'};
int n;
bool check(bool opt){
	stack<int> stk;
	int cnt=0;
	for (int i=1;i<=7;i++){
		if (a[i].st==0) stk.push(a[i].nd);
		else{
			if (stk.size()<2) return 0;
			int x=stk.top(); stk.pop();
			int y=stk.top(); stk.pop();
			if (a[i].nd==b[0]) stk.push(x+y);
			else if (a[i].nd==b[1]&&x-y>0) stk.push(x-y);
			else if (a[i].nd==b[2]) stk.push(x*y);
			else if (a[i].nd==b[3]){
				if (y==0) return 0;
				if (x%y==0) stk.push(x/y);
				else return 0;
			}
			else return 0;
			cnt++;
			if (opt&&cnt<=2) cout<<x<<(char(a[i].nd))<<y<<"="<<stk.top()<<"\n";
			else if (opt&&cnt==3) cout<<max(x,y)<<(char(a[i].nd))<<min(x,y)<<"="<<stk.top()<<"\n";
		}
	}
	if (stk.top()==24) return 1;
	return 0;
}
signed main(){
	ios::sync_with_stdio(0);
	for (int i=1;i<=4;i++) cin>>n, a[i]=mp(0,n);
	rep(i) rep(j) rep(k){
		a[5]=mp(1,b[i]), a[6]=mp(1,b[j]), a[7]=mp(1,b[k]);
		sort(a+1,a+8);
		do{
			if (check(0)){
				check(1);
				return 0;
			}
		}while(next_permutation(a+1,a+8));
	}
	cout<<"No answer!"; 
	return 0;
}
2025/2/2 21:30
加载中...