目录
11. 古典问题:生兔子
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
# coding: UTF-8
# 其实就是斐波那契数列
l = [1, 1]
for i in range(2, 13):
l.append(l[-1]+l[-2])
print l
12. 求素数
题目:判断101-200之间有多少个素数,并输出所有素数。
# coding: UTF-8
# 素数就是质数
import math
def is_sushu(n):
for i in range(2, int(math.sqrt(n)) + 1):
if n%i == 0:
return 0
return 1
count = 0
for i in range(101, 201):
if is_sushu(i) :
print i
count += 1
print math.sqrt(5)
print "total is %d" %(count)
13. 求水仙花数
题目:打印出所有的"水仙花数", 所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
# coding: UTF-8
import math
def yes(n):
one = n%10
ten = (n%100)/10
hundred = n/100
if math.pow(one, 3) + math.pow(ten, 3) + math.pow(hundred, 3) == n:
return 1
else :
return 0
for i in range(100, 1000):
if yes(i):
print i
14. 正整数分解质因数
题目:将一个正整数分解质因数。
例如:输入90,打印出90=2*3*3*5。
# coding: UTF-8
num = input('pls input a num: ')
l = []
i = 2
while i <> num:
if num % i == 0 :
l.append(i)
num = num/i
i = 2
else:
i += 1
l.append(num)
print l
def reduceNum(n):
print '{} = '.format(n),
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 xrange(2, n + 1) :
if n % index == 0:
n /= index
if n == 1:
print index
else :
print '{} *'.format(index),
break
n = input("pls input a num: ")
reduceNum(n)
15. 判断学生成绩等级
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
# coding: UTF-8
score = input('plz input your score: ')
if score >= 90:
level = 'A'
elif score >= 60:
level = 'B'
else :
level = 'C'
print '%d is in %s' %(score, level)
16. 输出指定格式的日期
题目:输出指定格式的日期。
# coding: UTF-8
import time
import datetime
print time.time()
print time.localtime()
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#输出今日日期
print(datetime.date.today()).strftime('%d/%m/%Y')
#创建日期对象
myday = datetime.date(1989, 10, 1)
print(myday.strftime('%d/%m/%Y'))
#日期算术运算
day2 = myday + datetime.timedelta(days=1)
print(day2.strftime('%d/%m/%Y'))
#日期替换
day3 = myday.replace(year = myday.year + 1)
print(day3.strftime('%d/%m/%Y'))
#day4 = myday + datetime.timedelta(months=1) # years,months is invalid in this func
#print(day4.strftime('%d/%m/%Y'))
day5 = myday.replace(day = myday.day + 1)
print(day5.strftime('%Y-%m-%d'))
day6 = myday.replace(month = myday.month + 1)
print(day6.strftime('%Y-%m-%d'))
17. 统计一个字符串中字符个数
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
# coding: UTF-8
l = raw_input("pls input a line: ")
count_char = 0
count_space = 0
count_num = 0
count_other = 0
for e in l:
# e = ord(e)
if e == ' ':
count_space += 1
elif (e >= 'A' and e <= 'Z') or (e >= 'a' and e <= 'z'):
count_char += 1
elif e >= '0' and e <= '9':
count_num += 1
else:
count_other += 1
print '英文字母、空格、数字和其它字符的个数分别是: %d, %d, %d, %d' %(count_char, count_space, count_num, count_other)
# ord(x) 讲一个字符转化为对应的ASCII码值
print '%d, %d' % (ord('A'), ord('a'))
def func():
l = raw_input("pls input a line: ")
count_char = 0
count_space = 0
count_num = 0
count_other = 0
for e in l:
if e.isspace():
count_space += 1
elif e.isalpha():
count_char += 1
elif e.isdigit():
count_num += 1
else:
count_other += 1
print '英文字母、空格、数字和其它字符的个数分别是: %d, %d, %d, %d' %(count_char, count_space, count_num, count_other)
func()
18. 求s=a+aa+aaa+aaaa+aa...a的值
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
# coding:UTF-8
# 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
import math
def main():
a = input("plz input a: ")
time = input("plz input time: ")
l = [a]
for i in range(1, time):
l.append(l[-1]+pow(10, i) * a)
print l
sum_num = 0
for i in l:
sum_num += i
print "s is %d" % sum_num
def add(x,y):
return x+y
def main2():
a = input("plz input a: ")
time = input("plz input time: ")
l = []
tmp = 0
for i in range(time):
tmp += a
a = a * 10
l.append(tmp)
print tmp
# reduce()函数会对参数序列中的元素进行累积。
# lambda匿名函数
l1 = reduce(lambda x,y: x+y, l)
l2 = reduce(add, l)
print l
print l1
print l2
main()
main2()
19. 求完数(完美数)
# 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
coding: UTF-8
# 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
def func(n):
tmp = []
for j in range(1,n):
if n%j == 0:
tmp.append(j)
# print n, tmp
if i == sum(tmp):
print n,tmp
return 1
else :
return 0
l = []
for i in range(1, 1001):
if func(i):
l.append(i)
print l
20. 自由落体
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
# coding:UTF-8
l = []
a = 100.0
for i in range(11):
l.append(a)
a = a/2
length = sum(l[:-2]) * 2 - l[0] -l[9]
print length, l[10]