P1236算24点!求调!急急急!80分!
查看原帖
P1236算24点!求调!急急急!80分!
889828
Wangbingxiang楼主2024/9/23 23:16
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,dp[4],t[5]={'0','+','-','*','/'};
int f(int x,int op,int y){
	if(op==1) return x+y;
	if(op==2) return max(x,y)-min(x,y);
	if(op==3) return x*y;
	if(op==4) {
		if(b==0 || a<b || a%b!=0){
			return -999;
		}else return a/b;
	}
}
int p(int x,int op,int y){
	int ans=f(x,op,y);
	cout<<max(x,y)<<(char)t[op]<<min(x,y)<<"="<<ans<<endl;
	return ans;
}
int main(){
	cin>>a>>b>>c>>d;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=4;j++){
			for(int k=1;k<=4;k++){
				if(f(f(f(a,i,b),j,c),k,d)==24) {p(p(p(a,i,b),j,c),k,d);return 0;}
				else if(f(f(a,i,b),j,f(c,k,d))==24) {p(p(a,i,b),j,p(c,k,d));return 0;}
				else if(f(f(a,i,f(b,j,c)),k,d)==24) {p(p(a,i,p(b,j,c)),k,d);return 0;}
				else if(f(a,i,f(b,j,f(c,k,d)))==24) {p(a,i,p(b,j,p(c,k,d)));return 0;}
				else if(f(a,i,f(f(b,j,c),k,d))==24) {p(a,i,p(p(b,j,c),k,d));return 0;}
			}
		}
	}
	cout<<"No answer!";
	return 0;
}

2024/9/23 23:16
加载中...