在数字 “123456789“ 中添加任意的 + 或 - 使得最终结果为:99

在数字 "123456789" 中添加任意的 + 或 - 使得最终结果为:99

 

#include <iostream>

#include <string>

#include <vector>

#define PLUS "+"

#define MINUS "-"

#define NONE ""

 

std::vector<std::string> g_vecOperator;

 

void GetAllString(std::string &str, int i, std::vector<std::string> &_m_vecList)

{

 

    if (i > 1)

    {

        for (int t = 0; t < g_vecOperator.size(); t++)

        {

            std::string NewStr = str;

            NewStr.insert(i, g_vecOperator[t]);

            GetAllString(NewStr, i - 1, _m_vecList);

        }

    }

    else if (i == 1)

    {

        for (int t = 0; t < g_vecOperator.size(); t++)

        {

            std::string EndStr = str;

            EndStr.insert(1, g_vecOperator[t]);

            _m_vecList.push_back(EndStr);

        }

    }

    return;

}

 

int CalculateString(std::string strCalculate)

{

    std::string strNum1{}, strNum{}, op1{}, op2{};

    int num1 = 0, num2 = 0;

    int i, StartIndex = 0;

   

    auto CalculateTwo = [&]()mutable {

          if (op1 == "+")

                {

                    num1 = num1 + num2;

                }

                else if (op1 == "-")

                {

                    num1 = num1 - num2;

                }

    };

    for (i = 0; i < strCalculate.size(); i++)

    {

        if (strCalculate[i] == '+' || strCalculate[i] == '-')

        {

            

            strNum = strCalculate.substr(StartIndex , i - StartIndex );

            StartIndex = i + 1;

            op2 = strCalculate[i];

             num2 = atoi(strNum.c_str());

            if (!op1.empty())

            {           

               CalculateTwo();

                op1 = op2;

            }

            else

            {

                num1 = num2;

                op1 = op2;

            }

        }

      

    }

    strNum = strCalculate.substr(StartIndex, i - StartIndex); //计算最后一个

    num2 = atoi(strNum.c_str());

    CalculateTwo();

    return num1;

}

 

int main()

{

    g_vecOperator.push_back(PLUS);

    g_vecOperator.push_back(MINUS);

    g_vecOperator.push_back(NONE);

    std::string str = "123456789";

    std::vector<std::string> m_vecList{};

    GetAllString(str, str.size() - 1, m_vecList);

    int i = 0;

    int SUM = 99;

    std::cout<<"在数字 \"" << str <<"\" 中添加任意的 + 或 - 使得最终结果为:"<<SUM<<std::endl;

    for (auto itStr : m_vecList)

    {

        if(CalculateString(itStr) == SUM)

        {

            std::cout<<itStr<<" = "<<SUM<<std::endl;

            i ++;

        }

    }

    std::cout<<"一共有"<<i<<"种结果"<<std::endl;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于只包含加号和减号的算式,我们可以通过添加括号来改变运算顺序,从而使得算式的值最大化。以下是一个使用贪心算法C++示例代码,用于找到最大值: ```cpp #include <iostream> #include <string> #include <stack> int maximizeExpressionValue(std::string expression) { std::stack<int> nums; std::stack<char> ops; for (char c : expression) { if (c == '+' || c == '-') { while (!ops.empty() && ops.top() == '+') { int num2 = nums.top(); nums.pop(); int num1 = nums.top(); nums.pop(); nums.push(num1 + num2); ops.pop(); } ops.push(c); } else { nums.push(c - '0'); } } while (!ops.empty()) { int num2 = nums.top(); nums.pop(); int num1 = nums.top(); nums.pop(); nums.push(num1 + num2); ops.pop(); } return nums.top(); } int main() { std::string expression; std::cout << "请输入只包含加号和减号的算式: "; std::cin >> expression; int maxValue = maximizeExpressionValue(expression); std::cout << "最大值为: " << maxValue << std::endl; return 0; } ``` 这段代码使用了两个栈,一个用于存储数字,另一个用于存储操作符。我们遍历算式的每个字符,如果是数字,则将其入栈;如果是操作符,则根据贪心策略,将栈数字与操作符进行运算,并将结果入栈。 最后,栈剩下的唯一一个数字就是算式的最大值。在这个例子,我们假设输入的算式格式正确,只包含加号和减号。如果输入包含其他字符或者无效格式,需要进行错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值