表达式求值

今天上课,老师教了小易怎么计算加法和乘法,乘法的优先级大于加法,但是如果一个运算加了括号,那么它的优先级是最高的。例如:

1

2

3

4

1+2*3=7

1*(2+3)=5

1*2*3=6

(1+2)*3=9

现在小易希望你帮他计算给定3个数a,b,c,在它们中间添加"+", "*", "(", ")"符号,能够获得的最大值。

//#include<stdc++.h>
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
int Max(int a, int b, int c)//暴力枚举法,列举每一种可能性。。。。
{
    vector<int> Count;
    int temp = 0;
    int count_1 = a + b + c;
    int count_2 = a*b*c;
    int count_3 = a*b*c;
    int count_4 = (a + b)*c;
    Count.push_back(count_1);
    Count.push_back(count_2);
    Count.push_back(count_3);
    Count.push_back(count_4);
    for (int i = 0; i < Count.size(); i++)
    {
        if (Count[i]>temp)
            temp = Count[i];
    }
    return temp;
}
int main()
{
    int q, w, e;
    int result;
    while (cin >> q >> w >> e)
    {
        result = 0;
        int count_1 = Max(q, w, e);
        int count_2 = Max(w, e, q);
        result = count_1 > count_2 ? count_1 : count_2;
        cout << result << endl;
    }
    //std::cin.get;
    system("pause");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值