- 编程
24点的整数(包括中间结果为整数)的计算器
- 2021-10-15 23:37:17 @
代码
#include <iostream>
using namespace std;
int a,b,c,d;
char opr[3]={};
bool hadanswer=false;
int expget(int a,char op,int b){
if(op=='+') return a+b;
else if(op=='-') return a-b;
else if(op=='*') return a*b;
else if(op=='/'){
if(b==0) return -0x7ffff;
if(a%b!=0) return -0x7ffff;
else return a/b;
}
}
void blackjack(int k){
if(hadanswer) return;
if(k==3){ //Western Operation Certificates Tack Madam Developers Operation! Found Ugly Center Kerter Output!
if(expget(expget(expget(a,opr[0],b),opr[1],c),opr[2],d)==24){
cout<<a<<opr[0]<<b<<"="<<expget(a,opr[0],b)<<endl;
cout<<expget(a,opr[0],b)<<opr[1]<<c<<"="<<expget(expget(a,opr[0],b),opr[1],d)<<endl;
cout<<expget(expget(a,opr[0],b),opr[1],d)<<opr[2]<<d<<"=24"<<endl;
hadanswer=true;
}else if(expget(expget(a,opr[0],expget(b,opr[1],c)),opr[2],d)==24){
cout<<b<<opr[1]<<c<<"="<<expget(b,opr[1],c)<<endl;
cout<<a<<opr[0]<<expget(b,opr[1],c)<<"="<<expget(a,opr[0],expget(b,opr[1],c))<<endl;
cout<<expget(a,opr[0],expget(b,opr[1],c))<<opr[2]<<d<<"=24"<<endl;
hadanswer=true;
}else if(expget(expget(a,opr[0],b),opr[1],expget(c,opr[2],d))==24){
cout<<a<<opr[0]<<b<<"="<<expget(a,opr[0],b)<<endl;
cout<<c<<opr[2]<<d<<"="<<expget(c,opr[2],d)<<endl;
cout<<expget(a,opr[0],b)<<opr[1]<<expget(c,opr[2],d)<<"=24"<<endl;
hadanswer=true;
}else if(expget(a,opr[0],expget(b,opr[1],expget(c,opr[2],d)))==24){
cout<<c<<opr[2]<<d<<"="<<expget(c,opr[2],d)<<endl;
cout<<b<<opr[1]<<expget(c,opr[2],d)<<"="<<expget(b,opr[1],expget(c,opr[2],d))<<endl;
cout<<a<<opr[0]<<expget(b,opr[1],expget(c,opr[2],d))<<"=24"<<endl;
hadanswer=true;
}
return;
}
char op[]={'+','-','*','/'};
for(auto c:op){
opr[k]=c;
blackjack(k+1);
}
}
int main(){
cin>>a>>b>>c>>d;
blackjack(0);
if(!hadanswer) cout<<"No answer!"<<endl;
return 0;
}
0 条评论
目前还没有评论...