练习11:

题目:

古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

分析:

兔子的规律为数列1,1,2,3,5,8,13,21....

程序:

f1 = 1
f2 = 1
for i in range(1, 22):
print('%d %d' % (f1, f2))
if (i % 3) == 0:
print()
f1 = f1 + f2
f2 = f1 + f2

输出结果:

1 1
2 3
5 8 13 21
34 55
89 144 233 377
610 987
1597 2584 4181 6765
10946 17711
28657 46368 75025 121393
196418 317811
514229 832040 1346269 2178309
3524578 5702887
9227465 14930352 24157817 39088169
63245986 102334155
165580141 267914296

练习12:

题目:

判断101-200之间有多少个素数,并输出所有素数。

分析:

判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。      

程序:

h = 0
leap = 1
from math import sqrt
from sys import stdout for m in range(101, 201):
k = int(sqrt(m + 1))
for i in range(2, k + 1):
if m % i == 0:
leap = 0
break
if leap == 1:
print('%-4d' % m)
h += 1
if h % 10 == 0:
print()
leap = 1
print('The total is %d' % h)

输出结果:

101
103
107
109
113
127
131
137
139
149 151
157
163
167
173
179
181
191
193
197 199
The total is 21

练习13:

题目:

打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

分析:

利用for循环控制100-999个数,每个数分解出个位,十位,百位。

程序:

for n in range(100, 1000):
i = n // 100
j = n // 10 % 10
k = n % 10
if n == i ** 3 + j ** 3 + k ** 3:
print(n)

输出结果:

153
370
371
407

练习14:

题目:

将一个正整数分解质因数。例如:输入90,打印出90=233*5。

分析:

对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:

(1) 如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2) 如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。

(3) 如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

程序:

def reduceNum(n):
print('{} = '.format(n),end="")
if not isinstance(n, int) or n <= 0 :
print('请输入一个正确的数字 !')
exit(0)
elif n in [1] :
print('{}'.format(n))
while n not in [1] : # 循环保证递归
for index in range(2, n + 1) :
if n % index == 0:
n //= index # n 等于 n//index
if n == 1:
print(index)
else : # index 一定是素数
print('{} * '.format(index),end="")
break
reduceNum(90)
reduceNum(100)

输出结果:

90 = 2 * 3 * 3 * 5
100 = 2 * 2 * 5 * 5

练习15:

题目:

利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

分析:

(a>b)?a:b这是条件运算符的基本例子。

程序:

score = int(input('输入分数:\n'))
if score >= 90:
grade = '优秀'
elif score >= 60:
grade = '良好'
else:
grade = '差劲'
print('分数:%d 等级:%s' % (score, grade))

输出结果:

输入分数:
98
分数:98 等级:优秀

练习16:

题目:

输出指定格式的日期。

分析:

使用 datetime 模块。

程序:

import datetime
if __name__ == '__main__':
# 输出今日日期,格式为 dd-mm-yyyy。更多选项可以查看 strftime() 方法
print(datetime.date.today().strftime('%d-%m-%Y'))
# 创建日期对象
date1 = datetime.date(1941, 1, 5)
print(date1.strftime('%d-%m-%Y'))
# 日期算术运算
date2 = date1 + datetime.timedelta(days=1)
print(date2.strftime('%d-%m-%Y'))
# 日期替换
date3 = date1.replace(year=date1.year + 1)
print(date3.strftime('%d-%m-%Y'))

输出结果:

23-03-2019
05-01-1941
06-01-1941
05-01-1942

练习17:

题目:

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

分析:

利用 while 或 for 语句,条件为输入的字符不为 '\n'。

程序:

import string

s = input('请输入一个字符串:\n')
letters = 0
space = 0
digit = 0
others = 0
i = 0
while i < len(s):
c = s[i]
i += 1
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print('char = %d,space = %d,digit = %d,others = %d' % (letters, space, digit, others))

输出结果:

请输入一个字符串:
qw12 3..?/
char = 2,space = 2,digit = 3,others = 4

练习18:

题目:

求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。

分析:

关键是计算出每一项的值。

程序:

from functools import reduce
Tn = 0
Sn = []
n = int(input('n = '))
a = int(input('a = '))
for count in range(n):
Tn = Tn + a
a = a * 10
Sn.append(Tn)
print(Tn)
Sn = reduce(lambda x, y: x + y, Sn)
print("计算和为:", Sn)

输出结果:

n = 5
a = 2
2
22
222
2222
22222
计算和为: 24690

练习19:

题目:

一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。

程序:

from sys import stdout

for j in range(2, 1001):
k = []
n = -1
s = j
for i in range(1, j):
if j % i == 0:
n += 1
s -= i
k.append(i) if s == 0:
print(j)
for i in range(n):
stdout.write(str(k[i]))
stdout.write(' ')
print(k[n])

输出结果:

6
1 2 3
28
1 2 4 7 14
496
1 2 4 8 16 31 62 124 248

练习20:

题目:

一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

程序:

tour = []
height = [] hei = 100.0 # 起始高度
tim = 10 # 次数 for i in range(1, tim + 1):
# 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下)
if i == 1:
tour.append(hei)
else:
tour.append(2 * hei)
hei /= 2
height.append(hei) print('总高度:tour = {0}'.format(sum(tour)))
print('第10次反弹高度:height = {0}'.format(height[-1]))

输出结果:

总高度:tour = 299.609375
第10次反弹高度:height = 0.09765625

最新文章

  1. SVG 文本
  2. 网站指纹识别工具——WhatWeb v0.4.7发布
  3. Create side-by-side stereo pairs in the Unity game engine
  4. Windows、Linux下文件操作(写、删除)错误的产生原因、及解决方法
  5. js-统计选项个数
  6. 基于阿里云容器服务用docker容器运行ASP.NET 5示例程序
  7. 20145222黄亚奇《Java程序设计》实验一实验报告
  8. [Entity Framework] MySQL @ Entity Framework 6
  9. Craking the coding interview 面试题:完美随机洗牌
  10. localstorage || globalStorage || userData
  11. WordPress插件制作笔记(三)---Stars Comments Article
  12. android 线程池
  13. 用async 解放你的大脑
  14. Angular.js学习范例及笔记
  15. linux 实现centos7在线升级最新版本内核
  16. Shell编程(二)Bash中调用Python
  17. SmartGit过期解决办法
  18. cocos creator踩坑日记
  19. Docker,win10
  20. JMS、AMQP和MQTT主要特性

热门文章

  1. @AliasFor 注解
  2. Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices
  3. priority_queue()大根堆和小根堆(二叉堆)
  4. hdu3706 Second My Problem First
  5. Find a multiple POJ - 2356 容斥原理(鸠巢原理)
  6. Codeforces Round #650 (Div. 3) C. Social Distance (前缀和)
  7. Python内置模块(你还在pip install time?)&amp;&amp; apt-get install -f
  8. 大数据开发-Spark-初识Spark-Graph &amp;&amp; 快速入门
  9. JVM ZeroTLAB 是什么意思呢?
  10. 【非原创】ZOJ - 4062 Plants vs. Zombies【二分】