算各种点数

来源:互联网 发布:补水保湿精华 知乎 编辑:程序博客网 时间:2024/04/30 08:06
#include<iostream>#include<string>#include<cstdlib>#include<cmath>#include<cstdio>using namespace std;const double Threshold=1E-6;const int CardsNumber=4;const int ResultValue=24;int number[6];string result[CardsNumber];bool PointsGame(int n){ if(n==1) { if(fabs(number[0]-ResultValue)<Threshold)  {   cout<<result[0]<<endl;   return true;  }  else  {  return false; } } for(int i=0;i<n;i++) {  for(int j=i+1;j<n;j++)  {   int a,b;   string expa,expb;  a=number[i];   b=number[j];   number[j]=number[n-1];   expa=result[i];   expb=result[j];   result[j]=result[n-1];   result[i]='('+expa+'+'+expb+')';   number[i]=a+b;   if(PointsGame(n-1))    return true;   result[i]='('+expa+'-'+expb+')';   number[i]=a-b;   if(PointsGame(n-1))   return true;result[i]='('+expb+'-'+expa+')';   number[i]=b-a;   if(PointsGame(n-1))   return true;    result[i]='('+expa+'*'+expb+')';    number[i]=a*b;    if(PointsGame(n-1))    return true;    if(b!=0)    {       result[i]='('+expa+'/'+expb+')'; number[i]=a/b;   if(PointsGame(n-1))   return true;    }    if(a!=0)    {    result[i]='('+expb+'/'+expa+')';    number[i]=b/a;    if(PointsGame(n-1))    return true; }    number[i]=a;    number[j]=b;    result[i]=expa;    result[j]=expb;  }}return false;}int main(){ int x; char ss[5]; int nn; int a,b,c,d; while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){ //if(nn==0) break; int flag=1;  //while(nn--){ //scanf("%s",ss); number[0]=a; number[1]=b; number[2]=c; number[3]=d;  for(int i=0;i<CardsNumber;i++) {  char buffer[20];  //cout<<"the "<<i<<"th number:";    //number[i]=ss[i]-'0';  x=number[i];  itoa(x,buffer,10);  result[i]=buffer; }  if(PointsGame(CardsNumber)) {    if(flag==1) flag=1;else flag=0;                 } else {     flag=0;   }if(flag==1) cout<<"Yes"<<endl;else if(flag==0) cout<<"No"<<endl;}  return 0;}

0 0
原创粉丝点击