今天上课,老师教了小易怎么计算加法和乘法,乘法的优先级大于加法,但是如果一个运算加了括号,那么它的优先级是最高的。例如:
1+2*3=7 1*(2+3)=5 1*2*3=6 (1+2)*3=9
现在小易希望你帮他计算给定3个数a,b,c,在它们中间添加"+", "*", "(", ")"符号,能够获得的最大值。
1+2*3=7 1*(2+3)=5 1*2*3=6 (1+2)*3=9
一行三个数a,b,c (1 <= a, b, c <= 10)
能够获得的最大值
1 2 3
9
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int[] n=new int[3]; for(int i=0;i<3;i++){ n[i]=sc.nextInt(); } Arrays.sort(n); int max1=n[0]+n[1]+n[2];//全1的情况 int max2=(n[0]+n[1])*n[2];//部分有1的情况,例如(1+2)*3 int max3=n[0]*n[1]*n[2];//无1的情况,例如2*3*4 System.out.print(Math.max(max1,Math.max(max2,max3))); } }
乱猜的规律,只要有1,那么就是两个最小的数加起来,再乘以第三个数,如果没有1,就是三个数相乘
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextInt()) { int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int [] array={a,b,c}; Arrays.sort(array); if(array[0]==1) {System.out.println((array[0]+array[1])*array[2]);} else { System.out.println(array[0]*array[1]*array[2]); } } } }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); int a[] = new int[3]; for (int i = 0; i < 3; i++) a[i] = cin.nextInt(); } Arrays.sort(a); int m1 = a[2] * a[1] * a[0]; int m2 = a[2] * (a[1] + a[0]); System.out.print(Math.max(m1, m2)); } }
import java.util.Scanner; import java.util.Arrays; //菜鸟一个,只能想到枚举法。[/苦笑/] public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int[] anwser = new int[5]; anwser[0] = a + b + c; anwser[1] = a * b * c; anwser[2] = (a + b) * c; anwser[3] = (a + c) * b; anwser[4] = (b + c) * a; Arrays.sort(anwser); System.out.println(anwser[4]); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); getMax(a, b, c); } private static void getMax(int a, int b, int c) { int num1 = (a + b * c) > (a * (b + c)) ? (a + b * c) : (a * (b + c)); int num2 = (a * b * c) > ((a + b) * c) ? (a * b * c) : ((a + b) * c); int max = num1 > num2 ? num1 : num2; System.out.println(max); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] params = input.nextLine().split(" "); int[] nums = new int[3]; for (int i = 0; i < 3; i++) { nums[i] = Integer.parseInt(params[i]); } System.out.println(Math.max(((nums[0] + nums[1]) * nums[2]), nums[0] * nums[1] * nums[2])); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(); String[] arr = a.split(" "); int[] I = new int[6]; I[0] = Integer.parseInt(arr[0]) * Integer.parseInt(arr[1]) * Integer.parseInt(arr[2]); I[1] = Integer.parseInt(arr[0]) + Integer.parseInt(arr[1]) + Integer.parseInt(arr[2]); I[2] = Integer.parseInt(arr[0]) + Integer.parseInt(arr[1]) * Integer.parseInt(arr[2]); I[3] = (Integer.parseInt(arr[0]) + Integer.parseInt(arr[1])) * Integer.parseInt(arr[2]); I[4] = Integer.parseInt(arr[0]) * Integer.parseInt(arr[1]) + Integer.parseInt(arr[2]); I[5] = Integer.parseInt(arr[0]) * (Integer.parseInt(arr[1]) + Integer.parseInt(arr[2])); Arrays.sort(I); System.out.println(I[I.length - 1]); } }最笨的办法,枚举出全部的可能,在输出最大值!菜鸟的解题思路!
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[3]; for(int i=0; i<3; i++) { arr[i]=sc.nextInt(); } Arrays.sort(arr); int max=0; if(arr[0]==1 && arr[1]==1 && arr[2]==1) max = 3; else if(arr[0]==1 && arr[2]>1) max = (arr[0]+arr[1])*arr[2]; else max = arr[0]*arr[1]*arr[2]; System.out.println(max); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int[] sort = new int[4]; sort[0] = a+b+c; sort[1] = a*b*c; sort[2] = (a+b)*c; sort[3] = a*(b+c); Arrays.sort(sort); System.out.println(sort[3]); } }
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
Main main = new Main();
System.out.println(main.calculateMax(a, b, c));
}
public int calculateMax(int a, int b, int c) { if (calcunlateTwo(calcunlateTwo(a,b), c) > calcunlateTwo(a, calcunlateTwo(b,c))) { return calcunlateTwo(calcunlateTwo(a,b), c); } else { return calcunlateTwo(a, calcunlateTwo(b,c)); } } public int calcunlateTwo(int a, int b) { if (a * b > (a + b)) { return a * b; } else { return a + b; } }
}
思路:先选出前两个数相加或相乘最大的数,
定义这个较大的数为max,
若第三个数为1,则返回max+1,否则返回max*第三个数。
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int max = Math.max(a + b, a * b); if (c == 1) { System.out.println(max + 1); } else { System.out.println(max * c); } } }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ int times = 3; int[] number = new int[times]; for(int i=0;i<times;i++){ number[i]= scan.nextInt(); } Main vo = new Main(); System.out.println(vo.getBigger(number[0], number[1], number[2])); } scan.close(); } int getBigger(int a,int b,int c){ int big = a*b*c; if((a+b)*c>big) big = (a+b)*c; if((a+c)*b>big) big = (a+c)*b; if((c+b)*a>big) big = (c+b)*a; return big; } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static int max1(int a, int b) { return Math.max(a + b, a * b); } public static int max2(int a, int b, int c) { return max1(max1(a, b), c); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] num = new int[3]; for (int i = 0; i < 3; i++) { num[i] = sc.nextInt(); } System.out.println(max2(num[0], num[1], num[2])); } }