牛客題解day01
阿新 • • 發佈:2018-11-01
今天上課,老師教了小易怎麼計算加法和乘法,乘法的優先順序大於加法,但是如果一個運算加了括號,那麼它的優先順序是最高的。例如:
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]); } }