python函数题目实战

  1. 1. Actual Combat Question
  2. 2. Question One
    1. 2.1. Complete Code
  3. 3. Question Two
    1. 3.1. Complete Code
  4. 4. Question Three
    1. 4.1. Complete Code
  5. 5. Question Four
    1. 5.1. Complete Code
  6. 6. Question Five
    1. 6.1. Complete Code

本次所涉及到的知识有:python定义函数(def)、调用函数、函数的参数、函数的返回值。

Actual Combat Question

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
2
3
4
5
6
7
8
9
# create two function in programe order to base
def max_divider():
# user input type int number
a = int(input("请输入第一个整数:"))
return a
def min_multipliter():
# user input type int number
b = int(input("请输入第一个整数:"))
return b

Great,after create two function,you need call these two function。

1
2
3
4
# call name max_divider function
c = max_divider()
# call name min_multipliter function
d = min_multipliter()

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
2
3
list = []
list1 = []
list2 = []

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
2
3
4
5
6
7
8
for i in range(c):
i += 1
if c % i == 0:
list1.append(i)
for i in range(d):
i += 1
if d % i == 0:
list2.append(i)

Decide alike number save in list

1
2
3
4
for i in list1:
# screen alike number
if i in list2:
list.append(i)

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
2
3
list = []
list1 = []
list2 = []

This question i by for loop from 1 to 1000 rang multiply user input number,after append in null list。

1
2
3
4
5
6
7
for i in range(1,1000):
# get max_divider from 1 to 1000 the product
e = c * i
list1.append(e)
# get min_multipliter from 1 to 1000 the product
f = d * i
list2.append(f)

Decide alike number save in list

1
2
3
4
for i in list1:
# screen alike number
if i in list2:
list.append(i)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def max_divider():
a = int(input("请输入第一个整数:"))
return a
def min_multipliter():
b = int(input("请输入第一个整数:"))
return b
c = max_divider()
d = min_multipliter()
list = []
list1 = []
list2 = []
for i in range(c):
i += 1
if c % i == 0:
list1.append(i)
for i in range(d):
i += 1
if d % i == 0:
list2.append(i)
for i in list1:
if i in list2:
list.append(i)
list.sort(reverse=True)
print("最大公约数为:",list[0])
list = []
list1 = []
list2 = []
for i in range(1,1000):
e = c * i
list1.append(e)
f = d * i
list2.append(f)
for i in list1:
if i in list2:
list.append(i)
print("最小公倍数为:",list[0])

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
2
3
daygetPeachNumber = 10
i = 1
Number10 = 1

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
2
3
4
5
6
7
8
while daygetPeachNumber > i:
# obtain peach the number in every days
Number = (Number10 + 1) * 2
# append result to list
list.append(Number)
# maintain previous result constant
Number10 = Number
daygetPeachNumber -= 1

Finally,reture result。

1
return list

For the obtain final result create null list list1 and use peachNumber function。

1
2
list1 = []
list1 = peachNumber()

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
2
3
4
5
i = 9
print("第10天的桃子数为:",1)
while i > 0:
print("第%d天的挑子数为:%d" %(i,list1[i-1]))
i -= 1

Complete Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def peachNumber():
daygetPeachNumber = 10
i = 1
Number10 = 1
list = []
while daygetPeachNumber > i:
Number = (Number10 + 1) * 2
list.append(Number)
Number10 = Number
daygetPeachNumber -= 1
return list
list1 = []
list1 = peachNumber()
list1.sort(reverse=True)
i = 9
print("第10天的桃子数为:",1)
while i > 0:
print("第%d天的挑子数为:%d" %(i,list1[i-1]))
i -= 1

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
2
# incoming one height parameter
def calculationHeight(height):

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
2
3
4
5
6
7
8
i = 1
# for storage final result
sum = 0
while i < 10:
height1 = (height / 2)
sum = sum + height + height1
height = height1
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
2
if i == 9:
sum += height

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
2
3
4
5
6
7
8
9
i = 1
sum = 0
while i <= 10:
height1 = (height / 2)
height = height1
if i == 10:
sum += height
i += 1
return sum

Use calculationHeight function and tenHeight function,print input final result。

1
2
3
4
5
# need for function incoming parameters(80、10)
a = calculationHeight(80)
b = tenHeight(10)
print("它在第10次落地时共经过%f" %a,"米")
print("它在第10次反弹%f" %b,"米")

Complete Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#求球在80米反弹的高度问题
def calculationHeight(height):
i = 1
sum = 0
while i < 10:
height1 = (height / 2)
sum = sum + height + height1
height = height1
if i == 9:
sum += height
i += 1
return sum
def tenHeight(num):
height = 80
i = 1
sum = 0
while i <= 10:
height1 = (height / 2)
height = height1
if i == 10:
sum += height
i += 1
return sum
a = calculationHeight(80)
b = tenHeight(10)
print("它在第10次落地时共经过%f" %a,"米")
print("它在第10次反弹%f" %b,"米")
# 它在第10次落地时共经过239.687500 米
# 它在第10次反弹0.078125 米

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#∑
def sigma(num):
sum = 0
a = 0
i = 1
f = 0
while i <= num:
for f in range(i+1):
a = a + f
sum = sum + a
a = 0
i += 1
return sum
a = sigma(int(input("请输入一个整数:")))
print("总和:",a)

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
2
3
4
5
6
7
8
9
list = []
for i in range(2,num):
fg = 0
for j in range(2,i-1):
if i % j == 0:
fg = 1
break
if fg == 0:
list.append(i)

Because question require even number than 6。

1
2
3
4
list1 = []
for i in range(6, num):
if i % 2 == 0:
list1.append(i)

Finally,by for loop and if judment decide this question and print result。

1
2
3
4
5
for i in list:
for h in list:
for j in list1:
if i + h == j:
print("%d + %d = %d"%(i,h,j))

Use ColdbachConjecture function。

1
GoldbachConjecture(int(input("请输入大于6的整数:")))

Complete Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#哥德巴赫猜想
def GoldbachConjecture(num):
list = []
for i in range(2,num):
fg = 0
for j in range(2,i-1):
if i % j == 0:
fg = 1
break
if fg == 0:
list.append(i)
list1 = []
for i in range(6, num):
if i % 2 == 0:
list1.append(i)
for i in list:
for h in list:
for j in list1:
if i + h == j:
print("%d + %d = %d"%(i,h,j))
GoldbachConjecture(int(input("请输入大于6的整数:")))