赞
踩
用4张扑克牌上的点数算24点是一个经典的游戏了。一般要求只允许使用加减乘除和括号进行四则运算。
例如:1,2,3,4 可以用表达式(1+2+3)*4 = 24 算出24。
如果用程序来实现的话有两种思路。
一是递归方式;一是后缀表达式(也称为逆波兰表达式)方法。
下面我们分别来介绍这两种方法。
- bool RecursiveCalc(double *pArray, int nCount)
- {
- bool bRet = false;
- if (1 == nCount)
- {
- if (fabs(pArray[0] - 24.0) < 0.0001)
- {
- g_Total++;
- Output(pArray[0]);
- return true;
- }
- else
- return false;
- }
- for (int i = 0; i < nCount; i++)
- {
- for (int j = 0; j < nCount; j++)
- {
- if (j == i)
- {
- continue;
- }
- double f1 = pArray[i];
- double f2 = pArray[j];
- char *p = g_op;
- for (int nop = 1; nop <= 4; nop++, p++)
- {
- if ('/' == *p && fabs(f2) < 0.0001)
- {
- continue;
- }
- double f = Operation(f1, f2, *p);
- double *pnew = new double[nCount-1];
- if (!pnew)
- {
- return bRet;
- }
- pnew[0] = f;
- for (int m = 1, n = 0; m < nCount-1; m++)
- {
- while (n == i || n == j)
- {
- n++;
- }
- pnew[m] = pArray[n++];
- }
- bRet |= RecursiveCalc(pnew, nCount-1);
- g_n2--;
- g_n1 -= 2;
- delete pnew;
- if (g_bOnlyGetOne && bRet)
- {
- return true;
- }
- }
- }
- }
- return bRet;
- }
- void SetNextSortPermutation(int *pIdx, int nLen)//调整到有序全排列的下一个排列
- {
- int nCurIdx = nLen-1;
- int nFindIdx = nCurIdx;
- while (nCurIdx-1 >= 0 && pIdx[nCurIdx-1] > pIdx[nCurIdx])
- {
- nCurIdx--;
- }
- if (nCurIdx-1 < 0)
- {
- return;
- }
- while (pIdx[nFindIdx] < pIdx[nCurIdx-1])
- {
- nFindIdx--;
- }
- int tmp = pIdx[nCurIdx-1];
- pIdx[nCurIdx-1] = pIdx[nFindIdx];
- pIdx[nFindIdx] = tmp;
- for (int i = nCurIdx, j = nLen-1; i < j; i++, j--)
- {
- tmp = pIdx[i];
- pIdx[i] = pIdx[j];
- pIdx[j] = tmp;
- }
- }
- void SetNextOpComb(int *nOp, int nLen)//调整到运算符组合的下一个组合,类似运算中的进位处理
- {
- int nCurIdx = nLen-1;
- while (nCurIdx >= 0 && 4 == ++nOp[nCurIdx])
- {
- nOp[nCurIdx] = 0;
- nCurIdx--;
- }
- }
- struct Node
- {
- int nType;
- union
- {
- double d;
- int op;
- };
- };
- bool CalcStack(Node *node_all, int nLen)
- {
- std::list<Node> lst;
- Node node;
- char output[200] = {0};
- char tmp[30] = {0};
- for (int i = 0; i < nLen; i++)
- {
- if (0 == node_all[i].nType)
- {
- lst.push_back(node_all[i]);
- sprintf_s(tmp, 30, "%2d ", (int)node_all[i].d);
- strcat_s(output, 200, tmp);
- }
- else if(1 == node_all[i].nType)
- {
- sprintf_s(tmp, 30, "%c ", g_op[node_all[i].op]);
- strcat_s(output, 200, tmp);
- Node f2 = lst.back();
- lst.pop_back();
- Node f1 = lst.back();
- lst.pop_back();
- if (abs(f2.d) < 0.0001 && 3 == node_all[i].op)
- {
- return false;
- }
- node.nType = 0;
- node.d = Operation(f1.d, f2.d, g_op[node_all[i].op]);
- lst.push_back(node);
- }
- else
- assert(0);
- }
- double f = lst.back().d;
- if (abs(f-24.0) < 0.0001)
- {
- g_Total++;
- printf("%s=%d\r\n", output, (int)f);
- return true;
- }
- return false;
- }
- int nCount = 0;
- for (int a = 1; a <= 10; a++)
- {
- for (int b = a; b <= 10; b++)
- {
- for (int c = b; c <= 10; c++)
- {
- for (int d = c; d <= 10; d++)
- {
- nCount++;
- }
- }
- }
- }
- 1 1 1 8 (( 1+ 1)+ 1)* 8 = 24
- 1 1 2 6 (( 1+ 1)+ 2)* 6 = 24
- 1 1 2 7 ( 1+ 7)*( 1+ 2) = 24
- 1 1 2 8 (( 1* 1)+ 2)* 8 = 24
- 1 1 2 9 ( 9- 1)*( 1+ 2) = 24
- 1 1 2 10 (( 1+ 1)+10)* 2 = 24
- 1 1 3 4 (( 1+ 1)* 3)* 4 = 24
- 1 1 3 5 ( 1+ 5)*( 1+ 3) = 24
- 1 1 3 6 (( 1+ 1)+ 6)* 3 = 24
- 1 1 3 7 (( 1* 1)+ 7)* 3 = 24
- 1 1 3 8 (( 1- 1)+ 3)* 8 = 24
- 1 1 3 9 ( 3+ 9)*( 1+ 1) = 24
- 1 1 3 10 (10-( 1+ 1))* 3 = 24
- 1 1 4 4 (( 1+ 1)+ 4)* 4 = 24
- 1 1 4 5 (( 1* 1)+ 5)* 4 = 24
- 1 1 4 6 (( 1- 1)+ 4)* 6 = 24
- 1 1 4 7 ( 7-( 1* 1))* 4 = 24
- 1 1 4 8 ( 4+ 8)*( 1+ 1) = 24
- 1 1 4 9 ( 1- 9)*( 1- 4) = 24
- 1 1 4 10 (( 1+ 1)*10)+ 4 = 24
- 6 6 7 10 - * - =24
- 6 8 6 - / 8 * =24
- 6 6 8 - 9 * - =24
- 6 6 8 + 10 - * =24
- 6 10 + 6 / 9 * =24
- 6 7 7 + 10 - * =24
- 6 8 9 7 - / * =24
- 6 7 * 8 10 + - =24
- 6 7 9 - 9 * - =24
- 10 7 - 10 * 6 - =24
- 8 6 - 8 * 8 + =24
- 8 8 + 6 / 9 * =24
- 6 8 10 8 - / * =24
- 8 6 9 9 + / / =24
- 6 8 10 - 9 * - =24
- 9 6 / 10 * 9 + =24
- 10 6 10 10 + - - =24
- 7 9 7 - * 10 + =24
- 8 7 9 - 8 * - =24
- 8 10 * 7 8 * - =24
- 8 9 10 7 - / * =24
- 7 10 8 - * 10 + =24
- 8 8 8 10 - * - =24
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。