zoukankan      html  css  js  c++  java
  • 快算24点,POJ(3983)

    题目链接:http://poj.org/problem?id=3983

    中文题,就不解释题意了。

    类似之前的一篇博客,这里也不上解释了,直接上代码吧。

    #include <iostream>
    #include <stdio.h>
    #include <string>
    using namespace std;
    
    char op[4]={'+','-','*','/'};
    
    double calc(double a,int opnum,double b)
    {
        switch(op[opnum])
        {
            case '+':
                return a+b;
                break;
            case '-':
                return a-b;
                break;
            case '*':
                return a*b;
                break;
            case '/':
                return a/b;
                break;
        }
        
    }
    
    bool calculator(int i, int j, int k, double a, double b, double c, double d)
    {
        if (calc(calc(a, i, b),j,calc(c, k, d)) - 24.0 == 0)
        {
            printf("(%.0lf%c%.0lf)%c(%.0lf%c%.0lf)
    ",a, op[i], b, op[j], c, op[k], d);
            return 1;
        }
        if (calc(calc(calc(a, i, b), j, c), k, d) - 24.0 == 0)
        {
            printf("((%.0lf%c%.0lf)%c%.0lf)%c%.0lf)
    ",a, op[i], b, op[j], c, op[k], d);
            return 1;
        }
        if (calc(calc(a, i, calc(b, j, c)), k, d) - 24.0 == 0)
        {
            printf("(%.0lf%c(%.0lf%c%.0lf))%c%.0lf)
    ",a, op[i], b, op[j], c, op[k], d);
            return 1;
        }
        if (calc(a, i, calc(calc(b, j, c), k, d)) - 24.0 == 0)
        {
            printf("%.0lf%c((%.0lf%c%.0lf)%c%.0lf)
    ",a, op[i], b, op[j], c, op[k], d);
            return 1;
        }
        if (calc(a, i, calc(b, j, calc(c, k, d))) == 24.0)
        {
            printf("%.0lf%c(%.0lf%c(%.0lf%c%.0lf))
    ",a, op[i], b, op[j], c, op[k], d);
            return 1;
        }
        return 0;
    }
    
    
    int main()
    {
        double a,b,c,d;
        while (scanf("%lf %lf %lf %lf", &a, &b, &c, &d) != EOF)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        if (calculator(i,j,k,a,b,c,d)==1)
                            goto over;
                    }
                }
            }
    
            over:continue;
        }
    
        return 0;
    }
  • 相关阅读:
    pyppeteer
    maven生命周期clean,compile,install,package区别
    centos7安装anyproxy
    安装jupyter notebook
    Linux-Centos7下安装Anaconda
    python文件 启动django项目
    PyCharm实用插件
    pyqt5 安装额外的工具
    PyQt5高级界面控件之QTableWidget的具体使用方法
    k8s Metrics Server 获取资源指标与 hpa 部署
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5514773.html
Copyright © 2011-2022 走看看