cal24

来源:互联网 发布:qb下载软件 编辑:程序博客网 时间:2024/05/22 15:44
// cala24.cpp : Defines the entry point for the console application.//#include "stdafx.h"#if 10// 1。根据算法1计算24点的代码#pragma warning( disable : 4786 )#include <iostream> #include <string> #include <cmath> #include <map>#include <time.h>using namespace std;const double PRECISION = 1E-6;      // 双精度小数的比较精度const int COUNT_OF_NUMBER  = 20;    // 运算数的最大允许数目double number[COUNT_OF_NUMBER];     // 运算数数组string expression[COUNT_OF_NUMBER]; // 运算表达式typedef map<string, bool>                   MAP_STRING;typedef pair<MAP_STRING::iterator, bool>    PAIR_MAPIT_B;int g_nNum = 0;         // 解的个数int g_nResult = 24;     // 运算结果 MAP_STRING  g_mapExp;   // 运算表达式唯一过滤容器。并保存所有运算表达式// n: 运算数的个数,bContinue: 是否求多解,为false,则只求得一个解就退出。bool Search(int n, bool bContinue);bool Search(int n, bool bContinue) {     if (n == 1) {        if ( fabs(number[0] - g_nResult) < PRECISION ) {            PAIR_MAPIT_B pib = g_mapExp.insert(pair<string, bool>(expression[0], true));            // 插入map成功,说明为不重复的解            if (pib.second) {                cout << expression[0] << endl;                 g_nNum ++;            }            return true;        }         else             return false;     }        double a, b;     string expa, expb;     for (int i = 0; i < n; i++) {         for (int j = i + 1; j < n; j++) {            a = number[i];             b = number[j];             number[j] = number[n - 1];                         expa = expression[i];             expb = expression [j];             expression[j] = expression[n - 1];                         expression[i] = '(' + expa + '+' + expb + ')';             number[i] = a + b;  // +            if (bContinue)                Search(n - 1, bContinue);            else                if ( Search(n - 1, bContinue) )  return true;                      number[i] = a - b; // -            if (number[i]>0) // 只要相减得正数的解,虽然得负数也正确            {                expression[i] = '(' + expa + '-' + expb + ')';                 if (bContinue)                    Search(n - 1, bContinue);                else                    if ( Search(n - 1, bContinue) )  return true;            }            else            {                expression[i] = '(' + expb + '-' + expa + ')';                 number[i] = b - a;  // 交换-                if (bContinue)                    Search(n - 1, bContinue);                else                    if ( Search(n - 1, bContinue) )  return true;            }                        //expression[i] = '(' + expa + '*' + expb + ')';             expression[i] = expa + '*' + expb;             number[i] = a * b; // 乘以            if (bContinue)                Search(n - 1, bContinue);            else                if ( Search(n - 1, bContinue) )  return true;                        if (b != 0) {                 //expression[i] = '(' + expa + '/' + expb + ')';                 expression[i] = expa + '/' + expb;                 number[i] = a / b;  // 除以                if (bContinue)                    Search(n - 1, bContinue);                else                    if ( Search(n - 1, bContinue) )  return true;            }             if (a != 0) {                 //expression[i] = '(' + expb + '/' + expa + ')';                 expression[i] = expb + '/' + expa;                 number[i] = b / a;  // 除,被除以                if (bContinue)                    Search(n - 1, bContinue);                else                    if ( Search(n - 1, bContinue) )  return true;            }               number[i] = a;             number[j] = b;             expression[i] = expa;             expression[j] = expb;         }     }     return false; }void main() {    do     {        // 初始化环境变量        g_nNum = 0;        g_nResult = 24;        g_mapExp.clear();                int nNumbers = 4;         //         cout << "请输入要计算的结果:" ;//         cin >> g_nResult; fflush(stdin);//         cout << "请输入数字个数:" ;//         cin >> nNumbers; fflush(stdin);                cout << "请输入" << nNumbers << "个数字(以空格分隔,回车结束):" ;                for (int i = 0; i < nNumbers; i++) {            char buffer[20];             int  x;             cin >> x;             number[i] = x;             itoa(x, buffer, 10);             expression[i] = buffer;         }        fflush(stdin);        Search(nNumbers, true);//         for (MAP_STRING::const_iterator ci = g_mapExp.begin(); ci != g_mapExp.end(); ++ci)//         {//             cout << ci->first << endl; //         }                        if (g_nNum > 0)            cout << "Success. " << g_nNum << endl;         else            cout << "Fail." << endl;     } while (1);}#endif