本次所涉及到的知识有:python定义函数(def)、调用函数、函数的参数、函数的返回值。
Actual Combat Question
(一)分别定义两个函数:max_divider、min_multipliter,返回两个数(从键盘输入的整数)中的最大公约数和最小公倍数。
举例如下所示:
1
2
3
4 请输入第1个整数:4
请输入第1个整数:6
最大公约数为:2
最小公倍数为:12(二) 猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃前一天剩下的一半零一个。到第十天早上想再吃时,见只剩下一个桃子了。求第一天共摘多少个桃子?编写函数得到猴子第一天桃子的数量。
(三)一只球从80m高度自由下落,每次落地后返回原高度的一半,在落下。求:它在第10次落地时共经过多少米?第10次反弹多高?编写函数实现。
(四)编写函数,求出1+(1+2)+(1+2+3)+····+(1+2+3+4+···+n)的和,函数以n为参数,n由用户从键盘输入。
(五)编写函数,验证哥德巴赫猜想,即任何一个大于6的偶数均可以表示成两个素数之和。
Question One
First,create two function in order to in base,because of programe require retrue two number and require is a max_divider function and min_multipliter function。
1 | # create two function in programe order to base |
Great,after create two function,you need call these two function。
1 | # call name max_divider function |
I use lists to solve this problem,so i will create three null list。first list (list)
in order to append fin ally result,second list (list1)
in order to append result in max_divider user input,third list (list2)
in order to append result in min_multipliter user input。
1 | list = [] |
OK,start structure max_divider (Greatest Common Divisor)
code。why is a Greatest Common Divisor? In short, the number that can divide a number is the divisor of the number, and the number that can be divided by a number is the multiple of the number。
Example:
number1 = 12(It’s divisible the number:1,2,3,4,6,12)
number2 = 18(It’s divisible the number:1,2,3,6,9,18)
Greatest Common Divisor = 6
So,i will make for loop ergodic variable c and variable d,after give zero to variable rang every number divide user input the number,decide these number divide variable c and variable b whether to be divisible by,last save in null list。
1 | for i in range(c): |
Decide alike number save in list
。
1 | for i in list1: |
Finally,by using sort method and flashback method, the result is obtained Greatest Common Divisor
。
1 | list.sort(reverse=True) |
After solve Greatest Common Divisor,now come to solve Least Common Multiple
。
What is a Least Common Multiple?the common multiple of two or more integers is called their common multiple, and the smallest common multiple except 0 is called the least common multiple of these integers。
Example:
number1 = 4(multiple of this number:4,8,12,16,20,24……)
number2 = 6(multiple of this number:6,12,18,24,30,36……)
Least Common Multiple = 12
is it exciting?yes this very interesting。same as the previous question create three list in order to solve Least Common Multiple the key,but functionally is alike。
1 | list = [] |
This question i by for loop from 1 to 1000 rang multiply user input number,after append in null list。
1 | for i in range(1,1000): |
Decide alike number save in list
。
1 | for i in list1: |
Finally ,because of get list in the smallest numberical value,so you don’t need using sort method。
1 | print("最小公倍数为:",list[0]) |
Complete Code
1 | def max_divider(): |
Question Two
The second question is a classic question in programming:Monkey Pick Peaches
This question the solving method use reverse reasoning
。by tenth day the peach number reverse reasoning first day peach number。
Create peachNumber function。
1 | def peachNumber(): |
In peachNumber function create daygetPeachNumber variable、i variable and Number10 variable,through these variable construct peachNumber function,achieve every day peach the number。
1 | daygetPeachNumber = 10 |
Create null list in order to append pick peach the number every days。
1 | list = [] |
At this time the most critical step has come,by the while loop obtain pick peach the number and will result append in list。
1 | while daygetPeachNumber > i: |
Finally,reture result。
1 | return list |
For the obtain final result create null list list1
and use peachNumber function。
1 | list1 = [] |
Because i think print for tenth day to first day the result,so i use sort method。if you hope for first day to tenth day print result,then you don’t need to use sort method。
1 | list1.sort(reverse=True) |
After by while loop print final result。
1 | i = 9 |
Complete Code
1 | def peachNumber(): |
Question Three
This question at first glance it looks a physics problem。
In fact,it is a simple physics problem,if you have not careful observed will step on the pit。
OK,first this question have two problem,i will use two function(calculationHeight
and tenHeight
) solve this two problem in this question。
Create first function calulation,solve how many meters did it pass on the 10th landing。
1 | # incoming one height parameter |
Because obtain total height,so find the height of each ball falling is divided by 2 on the basis of the first time and add。
1 | i = 1 |
After that,you think by the while enough loop ten times obtain result ?NO!
Here have a pit!if loop ten times obtain the result of the ball bounced on the tenth time。so ,we need loop nine times just fine。by if
judgment obtain nine time the number and add for variable sum。
1 | if i == 9: |
Return variable sum
,obtain how many meters did it pass on the 10th landing the height。
1 | return sum |
Create second function tenHeight,solve how high is the 10th rebound。
1 | def tenHeight(num): |
Definition variable height
assignment 80 the number and calculate the height of the ball。
1 | height = 80 |
After with same as the previous solution,fianlly by if
judment obtain tenth time ball the height。
1 | i = 1 |
Use calculationHeight function and tenHeight function,print input final result。
1 | # need for function incoming parameters(80、10) |
Complete Code
1 | #求球在80米反弹的高度问题 |
Question Four
The solution method of this question is similar to the second and third questions。by while
obtain user input need multiply the number,by for
obtain Sigma the effect。
Complete Code
1 | #∑ |
Question Five
Before doing this question we need know prime number
。
prime number
:a prime number refers to a natural number that has no other factors except 1 and itself among the natural numbers greater than 1。
Example:
3、5、7、11、13、17、19、23……..
Create ColdbachConjecture function and incoming parameters num。
1 | def GoldbachConjecture(num): |
Construct a simple judgment through the concept of prime numbers and for result append null list。
1 | list = [] |
Because question require even number than 6。
1 | list1 = [] |
Finally,by for
loop and if
judment decide this question and print result。
1 | for i in list: |
Use ColdbachConjecture function。
1 | GoldbachConjecture(int(input("请输入大于6的整数:"))) |
Complete Code
1 | #哥德巴赫猜想 |