今天上课,老师教了小易怎么计算加法和乘法,乘法的优先级大于加法,但是如果一个运算加了括号,那么它的优先级是最高的。例如:
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)
输出描述:
能够获得的最大值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()) {
int a=scanner.nextInt();
int b=scanner.nextInt();
int c=scanner.nextInt();
mathThis(a, b, c);
}
}
public static void mathThis(int a,int b,int c) {
int[] result=new int[5];
result[0]=a+b+c;
result[1]=a+b*c;
result[2]=a*(b+c);
result[3]=a*b*c;
result[4]=(a+b)*c;
for(int i=0;i<result.length;i++) {
for(int j=0;j<result.length-i-1;j++) {
if((j+1)>=result.length)
continue;
if(result[j]>result[j+1]) {
int temp=0;
temp=(int) result[j];
result[j]=result[j+1];
result[j+1]=temp;
}
}
}
System.out.println(result[4]);
}
}