资源简介

实现了24点游戏的算法,只需输入4个数字就可以打印有多少种算出24点的方法

资源截图

代码片段和文件信息

//////////////////////////////////////////////////////////////////////////

// 编程题目: 计算 24 点

// 编译环境: Visual C++ 6.0

// 程序设计: 廖增祥

//////////////////////////////////////////////////////////////////////////

#include 



//////////////////////////////////////////////////////////////////////////

// Process: 计算处理过程 递归函数

//                        result        [in]                当前计算结果

//                        a                [in]                原始计算数据

//                        fuhao        [in out]        要保存的符号数组

//                        nIndex        [in]                当前递归所在层[0 - 2]

int Process(int result int *a char *fuhao int nIndex)

{

        int temp;



        // 1. 计算加法

        temp = result + a[nIndex + 1];

        fuhao[nIndex] = ‘+‘;

        if(temp == 24 && nIndex == 2)

                return 1;



        if(nIndex < 2)

                if(Process(temp a fuhao nIndex + 1))

                        return 1;

        

        // 2. 计算减法

        temp = result - a[nIndex + 1];

        fuhao[nIndex] = ‘-‘;

        if(temp == 24 && nIndex == 2)

                return 1;

        if(nIndex < 2)

                if(Process(temp a fuhao nIndex + 1))

                        return 1;

        

        // 3. 计算乘法

        temp = result * a[nIndex + 1];

        fuhao[nIndex] = ‘*‘;



        if(nIndex != 0)

        {

                // 重新计算 temp 的值

                if(fuhao[nIndex - 1] == ‘+‘)

                {

                        temp = result - a[nIndex] + a[nIndex] * a[nIndex + 1];

                }

                else if(nIndex == 2 && fuhao[0] == ‘+‘ && (fuhao[1] == ‘*‘ || fuhao[1] == ‘/‘))

                {

                        temp = result - (result - a[0]) + (result - a[0]) * a[nIndex + 1];

                }



                else if(fuhao[nIndex - 1] == ‘-‘)

                {

                        temp = result + a[nIndex] - a[nIndex] * a[nIndex + 1];

                }

                else if(nIndex == 2 && fuhao[0] == ‘-‘ && (fuhao[1] == ‘*‘ || fuhao[1] == ‘/‘))

                {

                        temp = result + (result - a[0]) - (result - a[0]) * a[nIndex + 1];

                }

        }



        if(temp == 24 && nIndex == 2)

                return 1;



        if(nIndex < 2)

                if(Process(temp a fuhao nIndex + 1))

                        return 1;

        

        // 4. 计算除法

        int bContinue = 1;

        if(a[nIndex + 1] == 0)

                return 0;

        temp = result / a[nIndex + 1];

        fuhao[nIndex] = ‘/‘;



        if(nIndex != 0)

        {

                if(fuhao[nIndex - 1] == ‘+‘)

                {

                        // 重新计算 temp 的值

                        if(a[nIndex + 1] && a[nIndex] % a[nIndex + 1] == 0)

                   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-11-08 01:56  24point\
     文件        5514  2013-11-07 01:50  24point\24point.cpp
     文件        4296  2013-11-07 02:06  24point\24point.dsp
     文件         520  2013-11-06 23:13  24point\24point.dsw
     文件       41984  2013-11-08 01:55  24point\24point.ncb
     文件       48640  2013-11-08 01:55  24point\24point.opt
     文件         893  2013-11-07 01:50  24point\24point.plg
     目录           0  2013-11-07 01:50  24point\Debug\
     文件      184389  2013-11-07 01:50  24point\Debug\24point.exe
     文件      215180  2013-11-07 01:50  24point\Debug\24point.ilk
     文件        5710  2013-11-07 01:50  24point\Debug\24point.obj
     文件      203736  2013-11-06 23:21  24point\Debug\24point.pch
     文件      451584  2013-11-07 01:50  24point\Debug\24point.pdb
     文件       33792  2013-11-07 01:50  24point\Debug\vc60.idb
     文件       45056  2013-11-07 01:50  24point\Debug\vc60.pdb

评论

共有 条评论