24点是:一副牌中抽去大小王剩下52张牌),任意抽取4张牌,用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌必须用一次且只能用一次,如抽出的牌是3、8、8、9,那么算式为
  ( 9—8 ) ×8×3 
       (6-3)*10-6=24
      主要形式及方法:
  
  1.全部用加减法可以计算。(由于用技巧的思维惯性,这个容易疏忽);
  
  2.有1.2.3.4.6.8.12的牌,优先排除而算其他三张牌得到另一个符合的数;
  
  3. 2张牌得一个数,另外两张牌得一个数,两者运算得24;
  
  4. 比较特殊的(老师可能会考哦),要考虑分数的方法:举例 3,7,3,7
  
   答案为(3+3/7)*7 = 24;
  5. 大数 4,10,4,10答案为 (10*10-4)/4


网上代码

本人思路:(1)
四个数,选两个组合,产生第5个,在上下的两个和新产生的1个里再选两个,产生第6个,此时就有2个尚未用的(包括刚产生的),再次组合,产生结果,判断是不是24.注:每次组合有+/-/*/除四种情况。
不过写代码时思路错了点
错误代码


思路2:直接穷举,思路看了就明白了,郁闷的是提交老是超时(http://coder.buct.edu.cn/oj/Problem.aspx?pid=1634)郁闷.......屏蔽部分是输出的运算式

code

思路3:对4个数进行全列,每种排列对符号进行穷举(错误原因:依此计算,没考虑两两组合,所得结果再组合的情况)
#include<iostream>
#include 
<sstream>
#include 
<algorithm>
#include 
<cmath>
using namespace std;
double card[4];
double exp[
double ans,ans2,ans3,err=0.0000001
bool isSuc;
int i,j,k,c;
inline 
char getopr(int ope)
{
    
if(ope==0)
        
return '+';
    
else if(ope==1)
        
return '-';
    
else if(ope==2)
        
return '*';
    
else if(ope==3)
        
return '/';
    
else
         
return ' ';
}

inline 
double oper(double *d1,double *d2,int ope)
{
     
if(ope==0)
         
return (*d1)+(*d2);
     
else if(ope==1)
         
return (*d1)-(*d2);
     
else if(ope==2)
         
return (*d1) * (*d2);
     
else if(ope==3)
         
return (*d1)/(*d2);
     

}

void DFS()
{
    
for(i=0;i<4;i++)
    
{
        
if(i==3 && fabs(card[1]-24)<err)
            
continue;
        ans2
=oper(&card[0],&card[1],i);
        
for (j=0;j<4;j++)
        
{
            
if(j==3 && fabs(card[2]-24)<err)
            
continue;
           ans3
=oper(&ans2,&card[2],j);
           
if(ans3<1.84616 || ans3>312.0)
           
{
               
continue;
           }

           
for (k=0;k<4;k++)
           
{
               
if(k==3 && fabs(card[3]-24)<err)
                    
continue;
                
if(fabs(oper(&ans3,&card[3],k)-24.0)<err)
                
{
                    isSuc
=true;
                    printf(
"\n((%g%c%g)%c%g)%c%g=24",card[0],getopr(i),card[1],getopr(j),card[2],getopr(k),card[3]);
                    
return;
                }


           }

        }

    }

}

void search()
{
    
for(int m=0;!isSuc && m<23;m++)
    
{
        DFS();
        next_permutation(card,card
+4);
    }

}

int main()
{
    
//freopen("in.txt","r",stdin);
    
//freopen("out.txt","w",stdout);
    char isEnd;
    c
=0;
    
while(1)
    
{
        isEnd
=cin.peek();
        
if(isEnd=='E')
            
break;
        
//cin.putback(isEnd);
        isSuc=false;
        memset(card,
0,4);
        
for(i=0;i<4;i++)
        
{
            scanf(
"%lf",&card[i]);
        }

        search();
        
if(c++!=0)
            printf(
"\n");
        
if(isSuc)
        
{
            printf(
"YES");
        }

        
else
            printf(
"NO");
        cin.
get();
    }

    
return 0;
}
 


更多链接:
http://zhidao.baidu.com/question/79907925.html

http://www.3800hk.com/Article/cxsj/vc/bcjqvc/2005-08-06/Article_32541.html
Posted on 2009-06-10 21:20 北国飘雨 阅读(136) 评论(0)  编辑 收藏 引用 所属分类: ACM