牛客网 21天 3.2表达式求值

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

1

2

3

4

1+2*3=7

1*(2+3)=5

1*2*3=6

(1+2)*3=9

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

 

输入描述:

一行三个数a,b,c (1 <= a, b, c <= 10)


 

输出描述:

能够获得的最大值

示例1

输入

1 2 3

输出

9

 c++

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int a, b, c;
    cin>>a>>b>>c;
    int Max = a+b+c;
    Max = max(Max, a*b*c);
    Max = max(Max, a+b*c);
    Max = max(Max, (a+b)*c);
    Max = max(Max, a*b+c);
    Max = max(Max, a*(b+c));
    cout<<Max<<endl;
    return 0;
}

python

abc=list(raw_input().split(' '))
a=int(abc[0])
b=int(abc[1])
c=int(abc[2])
print max(a+b+c,a*b*c,a+b*c,a*b+c,(a+b)*c,a*(b+c))

java

import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int a = Integer.parseInt(line[0]);
        int b = Integer.parseInt(line[1]);
        int c = Integer.parseInt(line[2]);
        int max = 0;
        max = a + b + c;
        max = Math.max(max, a*b*c);
        max = Math.max(max, a+b*c);
        max = Math.max(max, (a+b)*c);
        max = Math.max(max, a*b+c);
        max = Math.max(max, a*(b+c));
        System.out.println(max);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值