This is the 16th day of my participation in Gwen Challenge
Examples of 21
The title
Monkey eat peach problem: the first day the monkey picked a number of peaches, immediately eat half, not addiction, and eat one more the next morning will be the rest of the peach half, and eat one more. For the rest of the day, I ate half and one each morning. In the morning of the 10th day, when I wanted to eat again, THERE was only one peach left. How much did you pick the first day?
Analysis of the
Take the reverse approach and work backwards.
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/3/31 20:42
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 21.py
# @Software: PyCharm
# @desc: Exercise Example 21
if __name__ == '__main__':
end = 1
start = 0
for day in range(10.1, -1):
start = (end + 1) * 2
end = start
print('Peaches of the first day:' + str(start))
Copy the code
The results of
Instances of 22
The title
Two ping-pong teams are playing three players each. Team A consists of a, B and C, and team B consists of X, Y and Z. The list of players has been drawn. The players were asked for the match list. A says he doesn’t compete with X, C says he doesn’t compete with X and Z. Please program to find out the names of the three teams.
Analysis of the
Three layers of circulation, and then add judgment can;
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/3/31 20:45
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 22.py
# @Software: PyCharm
# @desc: Exercise example 22
if __name__ == '__main__':
for i in range(ord('x'), ord('z') + 1) :for j in range(ord('x'), ord('z') + 1) :ifi ! = j:for k in range(ord('x'), ord('z') + 1) :if(i ! = k)and(j ! = k):if(i ! =ord('x')) and(k ! =ord('x')) and(k ! =ord('z')) :print('Match list: \na <-> %s\ NB <-> %s\ NC <-> %s' % (chr(i), chr(j), chr(k)))
Copy the code
The results of
Example 23
The title
Print the following pattern (diamond) :
*
***
*****
*******
*****
***
*
Copy the code
Analysis of the
First, the graph is divided into two parts, one rule for the first four lines, one rule for the last three lines, using a double for loop, the first layer controls the row, the second layer controls the column.
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 9:32
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 23.py
# @Software: PyCharm
# @desc: Exercise Example 23
if __name__ == '__main__':
for i in range(4) :for j in range(2 - i + 1) :print("", end=' ')
for k in range(2 * i + 1) :print("*", end=' ')
print(a)for i in range(3) :for j in range(i + 1) :print("", end=' ')
for k in range(4 - 2 * i + 1) :print("*", end=' ')
print(a)Copy the code
The results of
Examples of 24
The title
There is a sequence of fractions: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13… Find the sum of the first 20 terms in this sequence.
Analysis of the
If you look carefully at the numerator and denominator of a sequence, you can see that the numerator of the current fraction is equal to the sum of the numerator and denominator of the previous fraction, and the denominator of the current fraction is equal to the numerator of the previous fraction;
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 9:58
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 24.py
# @Software: PyCharm
# @desc: Exercise Example 24
if __name__ == '__main__':
numerator = 2
denominator = 1
sum = 0
for i in range(1.21) :sum += numerator * 1.0 / denominator
tmp = numerator
numerator += denominator
denominator = tmp
print('Sum of sequences:' + str(sum))
Copy the code
The results of
Examples of 25
The title
For 1 + 2! + 3! +… + 20! And.
Analysis of the
It’s similar to the accumulation, you compute the multiplication, and then you add;
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 10:02
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 25.py
# @Software: PyCharm
# @desc: Exercise Example 25
if __name__ == '__main__':
sum = 0
tmp = 1
for i in range(1.21):
tmp *= i
sum += tmp
print('1! + 2! + 3! +... + 20! = ' + str(sum))
Copy the code