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

Python algorithm practice week2 branch programming

編輯:Python

0x00 Branching structure

  • Problem presentation Enter two integers to store and a and b in , bring a The data stored in is less than b Data stored in .
  • analysis
    • Enter two numbers a and b
    • a It may be less than b It may also be greater than b
      • If a Less than b, Do nothing
      • conversely , In exchange for a and b
    • This type of problem requires different treatment for different situations , Questions like this , Sequence structure cannot be solved
  • Three basic forms of branching structure ( Single branch 、 Two branches and many branches )

Single branch structure

  • The grammatical form of a single branch structure
if ( Conditional expression ):
Sentence block
# The conditional expression can be any expression , As long as the result is not 0 The idea that True, Otherwise False
# Sentence block : It could be a statement , It can also be multiple statements 
  • The solution of the above problems
    • Restatement of the problem : Enter two integers to store and a and b in , bring a The data stored in is less than b Data stored in .
    • Python Language solution
# Enter two integers to store and a and b in , bring a The data stored in is less than b Data stored in .
# Use branch structure
a = int(input(' Please enter a:'))
b = int(input(' Please enter b:'))
print(' Before processing ')
print('a={},b={}'.format(a, b))
if a > b:
a, b = b, a # In exchange for a,b A variable's value
print(' After processing ')
print('a={},b={}'.format(a, b))

Two branch structure

  • The grammatical form of the double branch result
  • problem : Compare the size of two numbers , Enter two numbers , Output a large number
  • problem : Enter the length of the three sides , Judge whether it can form a triangle
  • problem : Rowing problem , A teacher takes a x A student went boating , Each ship can carry at most 4 personal , Ask how many ships are needed .

Multi branch structure

  • The grammatical form of a multi branch structure
  • problem : Calculate the value of the piecewise function , When x>1 when ,y=x, When x<-1 when ,y=-x, When x Be situated between -1 and 1 Between time ,y=1
    • algorithm flow chart
    • Python Language implementation
  1. = float(input(' Please enter x:'))
  2. x > 1: y = x
  3. x < -1: y = -x else: y = 1 print('y={}'.format(y))
  • problem : Know the percentage of a course mark, Convert it into a five grade system
  • problem : Known coordinate points (x,y), Judge its quadrant
## Known coordinate points (x,y), Judge its quadrant
x = float(input(' Please enter x Coordinate value :'))
y = float(input(' Please enter y Coordinate value :'))
if x == 0 and y == 0:
print(' The point is at the origin ')
elif x == 0:
print(' The point is at y On the shaft ')
elif y == 0:
print(' The point is at x On the shaft ')
elif x > 0 and y > 0:
print(' First quadrant ')
elif x < 0 and y > 0:
print(' Beta Quadrant ')
elif x < 0 and y < 0:
print(' The third quadrant ')
elif x < 0 and y > 0:
print(' Quadrant four ')
  • problem : Determine if a year is a leap year The condition for judging leap years is : The year can be 4 Divide but not be 100 to be divisible by , Or can be 400 to be divisible by .
    • flow chart
  • Python Language implementation
# Determine if a year is a leap year
# The condition for judging leap years is : The year can be 4 Divide but not be 100 to be divisible by , Or can be 400 to be divisible by .
# Method 1: Use a multi branch structure
y = int(input(' Please enter the year :'))
if y % 4 == 0 and y % 100 != 0:
print('{} Year is a leap year '.format(y))
elif y % 400 == 0:
print('{} Year is a leap year '.format(y))
else:
print('{} Year is not a leap year '.format(y))
# Method 2: With the help of logical operators
y = int(input(' Please enter the year :'))
if (y % 4 == 0 and y % 100 != 0) or (y % 400) == 0:
print('{} Year is a leap year '.format(y))
else:
print('{} Year is not a leap year '.format(y))
  • Discuss the following conditional expressions
if (y % 4 == 0 and y % 100 != 0) or (y % 400) == 0:
if (y % 4 == 0 and y % 100 ) or (y % 400) == 0:
if (not(y % 4) and y % 100 ) or (y % 400) == 0:

The above three conditional expressions all have the same effect , But the first one is more Simple and easy to understand

0x01 The three indicators of a good program

  • Simple and easy to understand
  • Extensibility is strong
  • Be faithful to your algorithm

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