程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python training camp] Python daily practice -- day 26: suffix expression

編輯:Python


Hello, Hello, my name is Dream ah , An interesting Python Blogger , Please go easy on me
2021 Blog star of the year TOP100,2021 Blog star of the year TOP5,Python Quality creators in the field , Welcome to cooperate with me ( At the end of the article VX Want to join the learning exchange group or Learning materials welcome +++)
Introductory notes : This paradise never lacks genius , Hard work is your final ticket !
Last , May we all shine where we can't see , Let's make progress together
“ Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place ”, It's me ! Ha ha ha ~

Preface :【Python Training camp 】 Is aimed at Python A war created by language learning Brush questions and revel party! If you don't grasp the solid foundation of knowledge , Welcome to this course :Python Public class It's best to use it together ~ If you like, hurry up and subscribe ! If you don't have self-control or motivation to learn and communicate together , Welcome to send a private letter or add my... At the end of the text VX, I will pull you into the learning exchange group , Let's exchange and study together , Group punch in

Python Practice every day :

  • Title Description
  • Their thinking
  • The source code to share
  • Learning summary
  • The articles ---- Good article recommends

Title Description

Title Description
Given N Plus sign 、M A minus sign and N + M + 1 It's an integer , Xiao Ming wants to know at all by this N Plus sign 、MM A minus sign and N + M +1 It's legal to round it up In the suffix expression , Which is the biggest result ?
Please output this maximum result .
For example, using 1 2 3 + -, be “2 3 + 1 -” The result of this suffix expression is 4, It's the biggest .

Input description
The first line contains two integers N , M

The second line contains N + M + 1 It's an integer A .

Output description
Output an integer , For the answer .

I/o sample
Example
Input
1 1
1 2 3
Output
4

 Operation limit
Maximum operation time :1s
Maximum running memory : 256M

Their thinking

At the beginning of this topic , We are also confused , How to discuss ± Parenthesized question , I have changed the example for several times and can't think of it , Until I found out Alex_996 It dawned on me , Here, I have learned from the experience of big men :2019 The 10th Blue Bridge Cup in - Provincial competition - C/C++ university B Group - I. Postfix expression

  • This question should first be clear , The input data can have negative numbers , This requires us to + perhaps - Consider the question of parentheses , How can we maximize the sum of the data we require .
  • The first is No, - Number When , Just sum the input data directly ;
  • When Yes - Number When it appears , Let's take an example to observe :a1 < a2 < a3 < a4 < a5 < a6.
  • One "-“ Number :(a1 + a2 + a3) - (a4 + a5 + a6)
    Two ”-“ Number :(a1 + a2 + a3) - (a4 + a5) - a6
    Three ”-“ Number :(a1 + a2 + a3) - a4 - a5 - a6
    four ”-“ Number :(a1 + a2) - (a4 - a3) - a5 - a6
    Five ”-" Number :a1 - (a5 - a2) - (a4 - a3) - a6
    We can find a magical thing , Top ones 5 All the formulas are equal , Are equivalent to a1 + a2 + a3 - a4 - a5 - a6. So in short , That is, when we only need to discuss a minus sign, we can solve all the problems of minus signs !
  • 1. If all numbers are positive : that (a2 + a3 + a4 + a5 + a6) - (a1) Is the biggest result
    2. If all numbers are negative : that (a6) - (a1 + a2 + a3 + a4 + a5) Is the biggest result , Equivalent to (abs(a1) + abs(a2) + abs(a3) + abs(a4) + abs(a5)) - abs(a6), With the first 1 Kind of similar .
    3. If there are positive and negative , that (a1 + a2 + a3) - (a4 + a5 + a6), among a1、a2、a3 Positive number ,a4、a5、a6 It's a negative number , Is the biggest result , Equivalent to (a1 + a2 + a3) + abs(a4) + abs(a5) + abs(a6)
  • So let's summarize : 1. If all numbers are positive : final a6 - a1 Equivalent to abs(a1 - a6)2. If all numbers are negative , It's also abs(a1) - abs(a6), Equivalent to abs(a1 - a6)3. If there are positive and negative : yes a1 + abs(a6), Equivalent to a1 - a6, It must be a positive number , So you can also add the absolute value number :abs(a1 - a6)

The source code to share

# Time : 2022/2/21 14:41
# File : Postfix expression .py
# Author : yes Dream ah !
# VX : Xu18300396393
# Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place !
N,M = map(int,input().split())
list1 = list(map(int,input().split()))
if M == 0:
print(sum(list1))
else:
sum = 0
list1.sort()
for i in range(1,N+M):
sum += abs(list1[i])
sum += abs(list1[-1]-list1[0])
print(sum)

Learning summary

  • Assign values to multiple variables in a single line :N,M = map(int,input().split())
  • Summarizing the rules will make a very complex subject very simple !

Today is my day Python The first of the training camp 26 God , I hope to see the best of you every day

The articles ---- Good article recommends

[Python Public class ] Zero base play Python The basic chapter ---- Section 1 :Python The introduction of

[Python Public class ] Zero base play Python Advanced ---- Section 1 :Python File operations in

Come on, let's have a problem swiping Carnival party Well !----【Python Training camp 】
All right. , That's all I want to share with you today
️️️ If you like , Don't be stingy with your one key triple connection ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved