加减乘除算24



描述:

算24游戏,只用加、减和乘的我们已经能够解决了,怎么把除法也加进来呢?最主要的问题是应对除以0的出错问题。

输入:

4个1~13之间的整数。

输出:

要求中间结果也都必须为整数的情况下,如果能得到24则输出“Yes”,否则输出“No”。

输入样例:

12 13 1 6  

输出样例:

Yes

#include<iostream>

using namespace std ;

int a[4] = {0} ;
int b[4] = {0} ;
int used[4] = {0} ;
int flag = 0 ;

void search(int m) ;
void judge() ;
int cal(int e , int f , int g) ;

int main()
{
  int i ;
  for(i = 0 ; i <= 3 ; i++)
  {
   cin >> b[i]  ;//用b[4]这个数组存储输入的四个数
  }
  search(0) ;
  if(flag == 1)
  {
   cout << "Yes" << endl ;
  }
  if(flag == 0)
  {
   cout << "No" << endl ;
  }
}
void search(int m)
{   int i ;
 if(m == 4)
 {
  judge() ;
 }
 for(i = 0 ; i <= 3 ; i++)
 {
  if(used[i] == 0)
  {
   a[m] = b[i]  ;//a[4]这个数组是对b[4]这个数组进行重新的排列
   used[i] = 1 ;
   search(m + 1) ;
   used[i] = 0;
  }
 }
}
void judge()
{
 int i , j , k , l ;//i是运算顺序 j,k,l是运算符  temp1 , temp2 是中间两次运算的结果
 int temp1 , temp2 ;

 for(i = 0 ; i <= 4 ; i++)
 {
  if(i == 0)//1 2 3
     {
      for(j = 0 ; j < 4 ; j++)
   {
     if(j == 3 && (a[1] == 0 || a[0] % a[1] != 0))//判断三次除数不为零且答案是整数!!!!!!!!!!!!!!!!!!
   {
    continue ;
   }
   temp1 = cal(a[0] , j , a[1]) ;
  
    for(k = 0 ; k < 4 ; k++)
    {
     if(k == 3 && (a[2] == 0 || temp1 % a[2] != 0))
      {
    continue ;
      }
      temp2 = cal(temp1 , k , a[2]) ;
     for(l = 0 ; l < 4 ; l++)
     {
      if(l == 3 && (a[3] == 0 || temp2 % a[3] != 0))
          {
         continue ;
          }
      if( cal(cal(cal(a[0] , j , a[1] ) , k , a[2]) , l , a[3]) == 24 )
      {
        flag =  1 ;
      }
     }
    }
   }
  }
  if(i == 1)//1 3 2
     {
       for(j = 0 ; j < 4 ; j++)
   {  
       if(j == 3 && (a[1] == 0 || a[0] % a[1] != 0))
   {
    continue ;
   }
   temp1 = cal(a[0] , j , a[1]) ;
    for(k = 0 ; k < 4 ; k++)
    {
     if(k == 3 && (a[3] == 0 || a[2] % a[3] != 0))
      {
    continue ;
      }
      temp2 = cal(a[2] , k , a[3]) ;
   for(l = 0 ; l < 4 ; l++)
   {
     if(l == 3 && (temp2 == 0 || temp1 % temp2 != 0))
        {
         continue ;
        }
     if( cal( cal(a[0] , j , a[1] ) , l , cal(a[2] , k , a[3])  ) == 24 )
      {
    flag =  1 ;
      }
   }
    }
   }
  }
  if(i == 2)//2 1 3
     {
      for(j = 0 ; j < 4 ; j++)
   {
     if(j == 3 && (a[2] == 0 || a[1] % a[2] != 0))
   {
    continue ;
   }
   temp1 = cal(a[1] , j , a[2]) ;
    for(k = 0 ; k < 4 ; k++)
    {
       if(k == 3 && (temp1 == 0 || a[0] % temp1 != 0))
        {
         continue ;
        }
        temp2 = cal(a[0] , k , temp1) ;
       
     for(l = 0 ; l < 4 ; l++)
     {
      if(l == 3 && (a[3] == 0 || temp2 % a[3] != 0))
         {
         continue ;
         }
      if( cal ( cal(a[0] , k , cal ( a[1] , j , a[2])), l , a[3]  ) == 24 )
      {
    flag =  1 ;
      }
     }
    }
   }
  }
  if(i == 3)//2 3 1
     {
      for(j = 0 ; j < 4 ; j++)
   {
    if(j == 3 && (a[2] == 0 || a[1] % a[2] != 0))
   {
    continue ;
   }
   temp1 = cal(a[1] , j , a[2]) ;
    for(k = 0 ; k < 4 ; k++)
    {
       if(k == 3 && (a[3] == 0 || temp1 % a[3] != 0))
        {
         continue ;
        }
        temp2 = cal(temp1 , k , a[3]) ;
     for(l = 0 ; l < 4 ; l++)
     {
      if(l == 3 && (temp2 == 0 || a[0] % temp2 != 0))
          {
         continue ;
          }
      if( cal ( a[0] , l ,cal(cal(a[1]  , j , a[2] ) , k , a[3])  ) == 24 )
      {
        flag =  1 ;
      }
     }
    }
   }
  }
  if(i == 4)//3 2 1
     {
      for(j = 0 ; j < 4 ; j++)
   {
     if(j == 3 && (a[3] == 0 || a[2] % a[3] != 0))
   {
    continue ;
   }
   temp1 = cal(a[2] , j , a[3]) ;
    for(k = 0 ; k < 4 ; k++)
    {
       if(k == 3 && (temp1 == 0 || a[1] % temp1 != 0))
        {
         continue ;
        }
        temp2 = cal(a[1] , k , temp1) ;
     for(l = 0 ; l < 4 ; l++)
     {
      if(l == 3 && (temp2 == 0 || a[0] % temp2 != 0))
         {
         continue ;
         }
      if( cal( a[0] , l , cal (a[1]  , k , cal(a[2] , j , a[3])) ) == 24 )
      {
    flag = 1 ;
      }
     }
    }
   }
  }
 }
}
int cal(int e , int f , int g)
{
 if(f == 0)
 {
  return (e+g) ;
 }
 if(f == 1)
 {
  return (e-g) ;
 }
 if(f == 2)
 {
  return (e*g) ;
 }
 if(f == 3)
 {
  return (e/g) ;
 }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值