python 表达式求值_表达式求值

28

推广到 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])

编辑于 2019-07-31 17:51:50

回复(7)

35

#include

#include

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;

}

发表于 2018-08-14 11:18:33

回复(2)

8

发表于 2018-10-11 16:30:13

回复(5)

7

只需要考虑1的存在就行,其他解法太复杂 a,b,c = map(int, input().split())

if a == 1:

b += 1

if c == 1:

b += 1

if b == 1:

if a < c:

a += 1

else:

c += 1

print(a*b*c)

发表于 2019-09-11 17:09:44

回复(1)

8

def fun():

a,b,c = list(map(int, input().split()))

M = max(a+b, a*b)

if c== 1:

return M + c

else:

return M * c

发表于 2019-08-03 11:08:53

回复(2)

8

//大致思路,不用考虑括号,最大值一定是由三个数里最大的一个数乘另外两个加或乘的最大值

#include

#include

#include

using namespace std;

int main()

{

int temp_number=0;

vector vect;

for(int i=0;i<3;i++)

{

cin>>temp_number;

vect.push_back(temp_number);

}

sort(vect.begin(),vect.end());

cout<

return 0;

}

发表于 2019-06-29 19:19:23

回复(8)

3

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]));

}

}

发表于 2019-06-30 15:34:49

回复(2)

4

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])

前面都太复杂,这题只有两种情况:

最小的数小于等于1时,那么最大的结果便是a+b的和再乘以c

若最小数大于1,则最大结果必然是三数的积

编辑于 2019-09-09 13:24:47

回复(6)

4

绞尽脑汁,得出的完美解答

#include

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;

}

发表于 2018-10-22 00:09:36

回复(0)

2

将本题推广到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))

编辑于 2020-06-15 14:11:59

回复(4)

2

思路:先选出前两个数相加或相乘最大的数,

定义这个较大的数为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);

}

}

}

编辑于 2019-07-22 15:44:26

回复(6)

3

没有人觉得这个题出得有问题吗????

并没有明确表示每个符号只能用一次,也没有说是否要完全部的符号。。。

是我脑子有问题吗?

发表于 2019-04-01 20:54:43

回复(6)

1

三个数;为最大的数*(剩余两个数的运算最大值)

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 

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]);

}

}

}

}

发表于 2020-08-29 15:34:29

回复(0)

1

都是正数,就这三种情况,其它的不可能为最大

#include 

using namespace std;

int a[3];

int main() {

for(int i=0;i<3;i++) cin >> a[i];

sort(a, a+3);

cout <

return 0;

}

发表于 2020-06-11 10:26:08

回复(0)

1

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 

nums[i] = Integer.parseInt(line[i]);

}

long res = 1;

for (int i = 0; i 

if (nums[i] == 1) {

if (i == 0) nums[i + 1]++;

else if (i == n - 1) nums[i - 1]++;

else if (nums[i - 1] 

else nums[i + 1]++;

}

}

for (int num : nums) res *= num;

System.out.println(res);

}

} java 推广到n个数,出现1就把1加到两边较小的那个数上,最后再累乘一遍即可。

发表于 2020-05-11 14:18:14

回复(0)

1

#include

(720)#include

using namespace std;

int main()

{

int num;

int ret=0;

while(cin>>num)

{

if(0==ret)

ret=num;

else

ret=max(ret*num,num+ret); //遇到1就是相加比较大

}

cout<

} 为什么你们的这么复杂

发表于 2020-04-28 22:58:09

回复(0)

1

#include

(720)#include

using namespace std;

int main()

{

int a,b,c;

cin>>a>>b>>c;

//直接枚举

int A[6]={0,0,0,0,0,0};

A[0]=a+b+c;

A[1]=a+b*c;

A[2]=a*b+c;

A[3]=a*b*c;

A[4]=a*c+b;

A[5]=(a+b)*c;

//排序后,直接输出最大值

sort(A,A+6);

cout<

return 0;

}

发表于 2020-04-09 21:51:10

回复(0)

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)));

}

}

发表于 2020-02-26 17:03:15

回复(0)

1

JavaScript(Node)😎题目:网易-表达式求值(枚举/dp)

//枚举 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)

}

})

发表于 2020-02-26 11:31:32

回复(0)

1

乱猜的规律,只要有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]);

}

}

}

}

发表于 2020-02-12 19:56:23

回复(0)

Python表达式求值是指根据给定的Python表达式计算出结果的过程。它涉及解析表达式,按照运算符的优先级和结合性进行计算,并返回最终的结果。 在上述引用中,提到了一种方法来编程演示如何一步一步地计算Python表达式。这种方法涉及使用ast模块解析表达式为抽象语法树(AST),然后逐步求值AST节点。首先,使用ast.parse()将字符串解析为AST。然后,找到一个首先要求值的节点,并使用eval(compile(node, '', 'eval'))对其求值。求值结果可以转换回AST节点,并用结果节点替换当前节点。接着,使用codegen.to_source从修改后的AST生成修改后的代码字符串,并继续相同的过程,直到树中只有一个节点。 这种方法可以逐步展示Python表达式的求值过程。它可以用于教学目的,帮助学生理解表达式的求值方式。然而,需要注意的是,这种方法可能无法处理某些复杂的行为,比如列表理解。在处理这些复杂情况时,可能需要采用其他方法或工具。 总结:Python表达式求值是根据给定的表达式计算结果的过程。使用ast模块可以解析表达式为AST,并通过逐步求值AST节点来展示求值过程。然而,对于复杂的行为,可能需要其他方法或工具来处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [逐步跟踪Python表达式求值](https://blog.csdn.net/weixin_30982943/article/details/112956371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值