今天上课,老师教了小易怎么计算加法和乘法,乘法的优先级大于加法,但是如果一个运算加了括号,那么它的优先级是最高的。例如:
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
推广到 n 个数
dp[i[[j] 代表 index 从 i 到 j (包含 i 和 j)中的最大值
所以:
dp[i][j] = max(dp[i][k] + dp[k+1][j],dp[i][k] * dp[k+1][j])for i <=k < j
nums = list(map(int,input().strip().split())) dp = [[0]*len(nums) for i in range(len(nums))] for i in range(len(dp)-1,-1,-1): for j in range(len(dp[0])): if i == j: dp[i][j] = nums[i] else: for k in range(i,j): dp[i][j] = max([dp[i][k] + dp[k+1][j],dp[i][k] * dp[k+1][j],dp[i][j]]) # print(dp) print(dp[0][len(nums)-1])
#include <iostream> #include <vector> using namespace std; double max1(double a, double b) { return max(a + b, a * b); } double max2(double a, double b, double c) { return max(max1(max1(a, b), c), max1(a, max1(b, c))); } int main() { double a = 0; double b = 0; double c = 0; while (cin >> a >> b >> c) { cout << max2(a, b, c) << endl; } return 0; }
importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args){Scanner sc = newScanner(System.in);inta = sc.nextInt();intb = sc.nextInt();intc = sc.nextInt();int[] d = newint[4];d[0] = a + b + c;d[1] = (a + b) * c;d[2] = a + (b + c);d[3] = a * b * c;Arrays.sort(d);System.out.println(d[3]);}}
//大致思路,不用考虑括号,最大值一定是由三个数里最大的一个数乘另外两个加或乘的最大值 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int temp_number=0; vector <int> vect; for(int i=0;i<3;i++) { cin>>temp_number; vect.push_back(temp_number); } sort(vect.begin(),vect.end()); cout<<vect[2]*max(vect[0]+vect[1],vect[0]*vect[1])<<endl; return 0; }
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])); } }
num_list = list(map(int, input().split())) num_list.sort() if num_list[0] <= 1: if len(set(num_list)) == 1: # 判断输入是否为特殊情况1 1 1 print(3) else: print((num_list[0] + num_list[1]) * num_list[2]) else: print(num_list[0] * num_list[1] * num_list[2])
绞尽脑汁,得出的完美解答 #include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int calc_res; int max = 0; calc_res = a + b + c; if (max < calc_res) max = calc_res; calc_res = a + b * c; if (max < calc_res) max = calc_res; calc_res = a * b + c; if (max < calc_res) max = calc_res; calc_res = a * b * c; if (max < calc_res) max = calc_res; calc_res = a * (b + c); if (max < calc_res) max = calc_res; calc_res = (a + b) * c; if (max < calc_res) max = calc_res; cout << max << endl; return 0; }
将本题推广到n个数,采用动态规划解法 给出我的一维动态规划代码 dp[i]表示前i 个数中构成最大的数 写出转化方程: dp[i] = max(nums[i]*dp[i-1], nums[i]+dp[i-1], dp[i-2]*(nums[i]+nums[i-1])) 如有不对的地方,还请多指正!输入格式: 第一行输入数字的个数n 第二行输入数字数组a1,a2,...,an def solution(nums, n): dp = [0]*(n) dp[0], dp[1] = nums[0], max(nums[0]+nums[1], nums[0]*nums[1]) for i in range(2, n): dp[i] = max(nums[i]*dp[i-1], nums[i]+dp[i-1], dp[i-2]*(nums[i]+nums[i-1])) return dp[-1] if __name__ == '__main__': while 1: n = int(input().strip()) nums = list(map(int, input().strip().split())) print(solution(nums, n))
思路:先选出前两个数相加或相乘最大的数,
定义这个较大的数为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.Arrays; import java.util.Scanner; public class 网易_2019_表达式求值 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int[] arr = new int[3]; for(int i = 0;i < 3;i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); int t1 = arr[0]+arr[1]; int t2 = arr[0]*arr[1]; if(t1>t2) { System.out.println(t1*arr[2]); }else { System.out.println(t2*arr[2]); } } } }
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[] nums = new int[line.length]; int n = line.length; for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(line[i]); } long res = 1; for (int i = 0; i < n; i++) { if (nums[i] == 1) { if (i == 0) nums[i + 1]++; else if (i == n - 1) nums[i - 1]++; else if (nums[i - 1] < nums[i + 1]) nums[i - 1]++; else nums[i + 1]++; } } for (int num : nums) res *= num; System.out.println(res); } }java 推广到n个数,出现1就把1加到两边较小的那个数上,最后再累乘一遍即可。
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))); } }
//枚举 Math.max() =>over const readline = require('readline') const rl = readline.createInterface({ input: process.stdin, ouput: process.stdout }) let inArr = [] rl.on('line',line=>{ if(!line) return inArr.push(line.trim()) if(inArr.length === 1){ let arr = inArr[0].split(' ').map(e=>+e) let a = arr[0], b = arr[1], c = arr[2] let res = Math.max(a+b+c,(a+b)*c,a+b*c,a*b+c,a*b*c,a*(b+c)) console.log(res) } })