24点 算法
1 #include "stdafx.h"
2 #include <iostream>
3 #include <functional>
4 #include <math.h>
5 #include <float.h>
6 #include <string>
7
8 using namespace std;
9
10 #if 0
11
12 double Add( double a,double b)
13 {
14 return a + b ;
15 };
16
17 double Sub( double a,double b)
18 {
19 return a - b ;
20 };
21 double BSub( double a,double b)
22 {
23 return b - a ;
24 };
25
26 double Muil( double a,double b)
27 {
28 return a * b ;
29 };
30
31 double Div( double a,double b)
32 {
33 return a / b ;
34 };
35 double BDiv( double a,double b)
36 {
37 return b / a ;
38 };
39
40 typedef double (*Fun)(double,double);
41
42 // 函数: bool IsEqual(float f1, float f2, int absDelta)
43 // 功能:把比较两个浮点数是否近似相同
44 // 输入:f1, f2参与比较的两个浮点数
45 // absDelta 两个浮点数之间允许有多少个其他可以精确表达的浮点数存在,相当于相对误差
46 // 输出: true,两个浮点数进行相等; false 两个浮点数不等
47 // 注意:仅仅适合IEEE 32位浮点数结构
48 // bool IsEqual(float f1, float f2, int absDelta)
49 // {
50 // int i1, i2;
51 // i1 = ( f1>0) ? ((int&)f1) : ( (int&) f1 - 0x80000000 );
52 // i2 = (f2>0) ? ((int&)f2) : ( (int&) f2 - 0x80000000 );
53 // return ((abs(i1-i2))<absDelta) ? true : false;
54 //
55 // }
56
57
58 #define FLOAT_EQ(x,v) (((v - EPSILON) < x) && (x <( v + EPSILON)))
59
60 int _tmain(int argc, TCHAR* argv[])
61 {
62
63
64 Fun vfun[6] = {Add,Sub,Muil,Div,BSub,BDiv};
65 std::string str[6] = {"+","-","*","/","被-","被/"};
66 //int av[4] = {1,5,5,5};
67 int av[4] = {8,8,3,3};
68 //int av[4] = {11,13,7,4};
69 //int av[4] = {8,3,1,1};
70
71
72 //cout<<IsEqual(56.342,56.342,1);
73 //cout<<FLOAT_EQ(56.342,56.342);
74
75 cout<<"\n";
76
77 for(int i = 0; i < 6; i++){
78 for(int j = 0; j < 6; j++){
79 for(int k = 0; k < 6; k++){
80 for(int x1 = 0; x1 <4; x1 ++){
81 for(int x2 = 0; x2 <4; x2 ++){
82 for(int x3 = 0; x3 <4; x3 ++){
83 for(int x4 = 0; x4 <4; x4 ++)
84 {
85 if(x1 != x2 && x1 != x3 && x1 != x4
86 && x2 != x3 && x2 != x4 && x3 != x4)
87 {
88 double sum1 = (vfun[i])(av[x1], av[x2]) ;
89 double sum2 = (vfun[j])(sum1 ,av[x3] ) ;
90 double sum3 = (vfun[k])(sum2,av[x4] ) ;
91 //if( fabs(sum3-24.0f) < 0.01f)
92 if( sum3 - 24.00 < 0.01 && 24.00 - sum3 < 0.01)
93 {
94 cout << av[x1] <<str[i] << av[x2] <<"="<< sum1<<"\n";
95 cout << sum1 <<str[j] << av[x3]<<"= "<<sum2<<"\n";
96 cout << sum2 <<str[k] << av[x4]<<"="<<sum3<<"\n";
97 cout <<"\n";
98 goto outfor7;
99 }
100
101 }
102 }
103 }
104 }
105 }
106 }
107 }
108 }
109 cout<<"imposbat";
110
111 outfor7:
112
113
114 cin.get();
115
116 return 1;
117 }
118 #endif
函数对象
1 #include "stdafx.h"
2 #include <iostream>
3 #include <functional>
4 #include <boost/array.hpp>
5 #include <boost/function.hpp>
6
7 using namespace std;
8 using namespace boost;
9
10 template<class _Ty>
11 struct nminus
12 : public binary_function<_Ty, _Ty, _Ty>
13 { // functor for operator-
14 _Ty operator()(const _Ty& _Left, const _Ty& _Right) const
15 { // apply operator- to operands
16 return (_Right - _Left);
17 }
18 };
19
20 template<class _Ty>
21 struct ndivides
22 : public binary_function<_Ty, _Ty, _Ty>
23 { // functor for operator/
24 _Ty operator()(const _Ty& _Left, const _Ty& _Right) const
25 { // apply operator/ to operands
26 return (_Right / _Left);
27 }
28 };
29
30
31 inline int GetExpoBase2(double d)
32 {
33 int i = 0;
34 ((short *)(&i))[0] = (((short *)(&d))[3] & (short)32752); // _123456789ab____ & 0111111111110000
35 return (i >> 4) - 1023;
36 }
37
38 bool Equals(double d1, double d2)
39 {
40 if (d1 == d2)
41 return true;
42 int e1 = GetExpoBase2(d1);
43 int e2 = GetExpoBase2(d2);
44 int e3 = GetExpoBase2(d1 - d2);
45 if ((e3 - e2 < -48) && (e3 - e1 < -48))
46 return true;
47 return false;
48 }
49
50 int _tmain(int argc,_TCHAR* argv[])
51 {
52 //cout<<plus(1,5);
53 //error plus 是一个类!!!
54 {
55 boost::function<double (double,double)> fun;
56 // fun = negate< minus<double>() >();
57 // cout<< (negate<double>())( (minus<double>())(2,89) );
58 //cout<<fun(2,89)<<"\n";
59 }
60
61 {
62 array< function<double (double,double )> ,6> funlist =
63 { plus<double>() , minus<double>() ,multiplies<double>() , divides<double>(), nminus<double>(), ndivides<double>() };
64 array<std::string,6> name = {"+","-","*","/","b_","b/"};
65 array<double,4> num= {5,3,3,3};
66 //array<double,4> num= {5,5,5,1};
67 //array<double,4> num= {8,8,3,3};
68 //array<double,4> num= {8,3,3,3};
69
70 for(int i = 0; i < funlist.size(); i++){
71 for(int j = 0; j < funlist.size(); j++){
72 for(int k = 0; k < funlist.size(); k++){
73 for(int x1 = 0; x1 <4; x1 ++){
74 for(int x2 = 0; x2 <4; x2 ++){
75 for(int x3 = 0; x3 <4; x3 ++){
76 for(int x4 = 0; x4 <4; x4 ++){
77 if(x1 != x2 && x1 != x3 && x1 != x4
78 && x2 != x3 && x2 != x4 && x3 != x4)
79 {
80 double sum1 = funlist[i]( num[x1], num[x2]) ;
81 double sum2 = funlist[j]( sum1, num[x3]);
82 double sum3 = funlist[k]( sum2,num[x4]);
83
84 double sum4 = funlist[i]( num[x1], num[x2]) ;
85 double sum5 = funlist[j]( num[x3], num[x4]);
86 double sum6 = funlist[k]( sum4, sum5 );
87
88 // static int ncoutstep = 0;
89 // ncoutstep++;
90 // cout<<sum3<<" ";
91 // if(ncoutstep % 7 == 0)
92 // cout<<endl;
93
94 if( Equals(sum3,24.0) || sum3 == 24 )
95 {
96 cout << num[x1] <<name[i] << num[x2] <<"="<< sum1<<"\n";
97 cout << sum1 <<name[j] << num[x3]<<"= "<<sum2<<"\n";
98 cout << sum2 <<name[k] << num[x4]<<"="<<sum3<<"\n";
99 cout <<"\n";
100 goto outfor7;
101 }
102
103 if( Equals(sum6,24.0) || sum6 == 24 )
104 {
105 cout << num[x1] <<name[i] << num[x2] <<"="<< sum4<<"\n";
106 cout << num[x3] <<name[j] << num[x4]<<"= "<<sum5<<"\n";
107 cout << sum4 <<name[k] << sum5 <<"="<<sum6<<"\n";
108 cout <<"\n";
109 goto outfor7;
110 }
111
112 }
113 }
114 }
115 }
116 }
117 }
118 }
119 }
120 cout<<"imposbat";
121 outfor7:
122 ;
123 }
124 cin.get();
125 return 1;
126 }
24点算 exe函数对象的版本更正确些