Python uses natural language/flow chart to describe the algorithm and implement it!

Definition and function of algorithm

1. Import: Steps to solve problems by programming

  1. analyse problem
  2. Divide the boundary
  3. Design algorithm#importance
  4. Programming
  5. Debug test
  6. post-maintenance

Note: Refer to "Python Language Programming" by Song Tian

2. Algorithmic problems in life: changing drinks

Two cups, one cup for Coke and one cup for Sprite. How can I make the cup for Coke and Sprite for Coke?

Get another cup.

  1. Design algorithm:
    t=a #first step
    a=b #second step
    b=t #third step
  2. Write code:
>>> a="可乐"
>>> b="雪碧"
>>> t=a
>>> a=b
>>> b=t
>>> print("a=",a,"b=",b)
a= 雪碧 b= 可乐
1234567

Complete the process from algorithm to program.

Definition of algorithm:
Steps to solve a problem An algorithm is a set of well-defined rules used to solve a problem in a limited number of steps.

How to describe the algorithm

1. Natural language describes the problem of chicken and rabbit in the same cage

The ancient Chinese mathematics book "Sun Tzu Suanjing" should say "Today there are pheasant rabbits in the same cage, with thirty-five heads on the top and ninety-four legs on the bottom. How do the pheasant rabbits have each other?"

  1. Mathematical thinking: hypothetical method, equations
  2. Computational Thinking: Exhaustive Method

List the combinations of chickens and rabbits, and keep trying and making mistakes.
There are limits to trial and error.
Define the number of chickens as the variable a, then the rabbit is 35-a. Whether the sum of the number of chickens and rabbit feet is 94 is the criterion for the end of the program

2. Natural language describes the two basic elements of the algorithm description of the chicken and rabbit cage problem
: the initial state and the law of change.

  1. Initial state: the first set of data (0 chickens, 35 rabbits), set chicken as variable n and rabbit as variable 35-n.
  2. The law of change: the number of chickens after failure is +1, and the number of rabbits is -1. The law of change n=n+1.

Therefore: natural language description

  1. Suppose the number of chickens is n; the number of rabbits is 35-n;
  2. n=0;
  3. If 2 n+4 (35-n)=94, output the result n; otherwise, go to step 4;
  4. n=n+1;
  5. Go to step 3;

2. The flow chart describes the problem of chicken and rabbit in the same cage:

Three, code implementation

Basic realization:

#鸡兔同箱V1: 
n=0 
m=35-n #Set the initial value, there are 0 chickens and 35 rabbits 
while 0<=n<=35 and 0<=m<=35:   
    if 2*n +4*m == 94: 
        #Table judgment print("chickens",n,"only") 
        print("rabbits",m,"only") 
        break 
    else: 
        n=n+1 #the number of chickens is added One, reduce the number of rabbits by one, keep trying and wrong 
        m=35-n 
#until the correct answer 1234567891011

operation result:

There are 23 chickens
and 12 rabbits

Can see the process:

n=0 
m=35-n #Set the initial value, there are 0 chickens and 35 rabbits 
while 0<=n<=35 and 0<=m<=35:   
    if 2*n+4*m == 94 : #表 Judgment 
        print("Meet!") 
        print("Chickens",n,"only") 
        print("Rabbits",m,"only") 
        break 
    else: 
        n=n+1 #The number of chickens plus One, reduce the number of rabbits by one, keep trying and wrong 
        m=35-n #until you get the correct answer 
        print("chicken has",n,"only,","rabbit has",m,"only when,"," Are you eligible?") 
123456789101112

operation result:

When there are 1 chicken and 34 rabbits, are they eligible?
When there are 2 chickens and 33 rabbits, are they eligible?
When there are 3 chickens and 32 rabbits, are they eligible?
When there are 4 chickens and 31 rabbits, are they eligible?
When there are 5 chickens and 30 rabbits, are they eligible?
When there are 6 chickens and 29 rabbits, are they eligible?
When there are 7 chickens and 28 rabbits, are they eligible?
When there are 8 chickens and 27 rabbits, are they eligible?
When there are 9 chickens and 26 rabbits, are they eligible?
When there are 10 chickens and 25 rabbits, are they eligible?
When there are 11 chickens and 24 rabbits, are they eligible?
When there are 12 chickens and 23 rabbits, are they eligible?
When there are 13 chickens and 22 rabbits, are they eligible?
When there are 14 chickens and 21 rabbits, are they eligible?
When there are 15 chickens and 20 rabbits, are they eligible?
When there are 16 chickens and 19 rabbits, are they eligible?
When there are 17 chickens and 18 rabbits, are they eligible?
When there are 18 chickens and 17 rabbits, are they eligible?
When there are 19 chickens and 16 rabbits, are they eligible?
When there are 20 chickens and 15 rabbits, are they eligible?
When there are 21 chickens and 14 rabbits, are they eligible?
When there are 22 chickens and 13 rabbits, are they eligible?
When there are 23 chickens and 12 rabbits, are they eligible?
meets the!
There are 23 chickens
and 12 rabbits

Try to change the number of heads and feet of chickens and rabbits to a pattern that can be entered:

a=int(input("How many heads are there?")) 
b=int(input("How many feet are there:")) 
n=0 
m=an 
while 0<=n<=a and 0< =m<=a: 
    if 2*n+4*m == b: 
        print("============================ =====================") 
        print("chickens",n,"only") 
        print("rabbits",m,"only") 
        break 
    else: 
        n=n+1 
        m=an 
       #print("When the number of chickens is:",n,"the number of rabbits is:",m,"does not meet the conditions!") 
1234567891011121314

operation result:

How many heads are there in total: 24
How many feet are there in total: 84
================================== ===============
6 chickens
and 18 rabbits

Propose changes:

When inputting the number of heads and feet can not find the answer, the above program will not report an error.
You can use judgment sentences to achieve this function.

It's not as difficult as you think. Want to learn deep learning? Learn Python first! Get the getting started tutorial here

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/109072714