zoukankan      html  css  js  c++  java
  • 24点游戏

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    typedef struct BiTNode
    {
        char date[4];
        struct BiTNode *lchild, *rchild;
    }BiTNode, *BiTree;
    
    int creB(BiTree &Q)
    {
        Q = new BiTNode;
        if (~scanf_s("%s", Q->date, 4) && Q->date[0] != '#')
        {
            if (Q->date[0] == '#')
                Q = NULL;
            else
            {
                creB(Q->lchild);
                creB(Q->rchild);
            }
            return 1;
        }
        Q = NULL;
        return -1;
    }
    
    double get_num(char *a)
    {
        int length;
        int i;
        double result = 0;
        if (a[0] == 'A')
            return 1;
        else if (a[0] == 'J')
            return 11;
        else if (a[0] == 'Q')
            return 12;
        else if (a[0] == 'K')
            return 13;
        else
        {
            length = strlen(a);
            for (i = 0; i < length; i++)
            {
                result = result * 10 + a[i] - '0';
            }
        }
        return result;
    }
    
    double priB(BiTree Q)
    {
        double num = 0;
        if (Q == NULL)
            return num;
        if (Q->date[0] == '/' || Q->date[0] == '*' || Q->date[0] == '-' || Q->date[0] == '+')
        {
            double i, j;
            i = priB(Q->lchild);
            j = priB(Q->rchild);
            switch (Q->date[0])
            {
            case '+':
                num = i + j;
                break;
            case '-':
                num = i - j;
                break;
            case '/':
                if (i == 0 || j == 0)
                    return 0;
                num = i / j;
                break;
            case '*':
                num = i * j;
                break;
            }
        }
        else
            return get_num(Q->date);
        return num;
    }
    
    void priA(BiTree Q)
    {
    
        if (Q == NULL)
            return;
        if (Q->date[0] == '/' || Q->date[0] == '*' || Q->date[0] == '-' || Q->date[0] == '+')
        {
            printf("(");
            priA(Q->lchild);
            printf("%s", Q->date);
            priA(Q->rchild);
            printf(")");
        }
        else
            printf("%s", Q->date);
    }
    
    
    int main()
    {
        BiTree Q;
        while (creB(Q) == 1)
        {
            double k;
            k = priB(Q);
            if (k ==24||k== 23.999999999999989)
            {
                priA(Q);
                printf("=%d
    ", 24);
            }
            else
                printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    hdu6514 // 二维差分 二维前缀和 基本容斥 压维
    【模板】欧拉函数 & 欧拉筛
    并查集模板 hdu1703
    大数 gcd
    hdu1087 dp
    洛谷p1306 斐波那契公约数
    hdu6814 Tetrahedron 线性求逆元 + 快速幂求逆元
    hdu6867 Tree // DFS
    hdu6869 Slime and Stones // 威佐夫博弈&#183;改
    Python实现EXCEL表格的排序功能
  • 原文地址:https://www.cnblogs.com/1328497946TS/p/11063642.html
Copyright © 2011-2022 走看看