好吧,开始第五章习题:
5-2.又是老调重谈,raw_input这个函数真是非常麻烦,返回是str类型,没办法,只能在函数里面使用int内建。(就只是一道简单的两数相乘,哭了)
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 def mul(a,b): 5 return int(a)*int(b) 6 7 if __name__ == '__main__': 8 a = raw_input("输入a:") 9 b = raw_input("输入b:") 10 print mul(a,b)
5-3.谭浩强的书见过无数遍的题目,输入分数看A-F,没什么好说的,但是想练一下用字典这个数据结构,写来看看
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 def grade(a): 5 aDist = {0:'F',1:'F',2:'F',3:'F',4:'F',5:'F',\ 6 6:'D',7:'C',8:'B',9:'A',10:'A'} 7 return aDist[int(a) / 10] 8 9 if __name__ == '__main__': 10 g = raw_input("请输入测验成绩:") 11 print grade(g)
5-4 判断是否闰年,无难度
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 def Leapyear(a): 5 year = int(a) 6 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): 7 return True 8 else: 9 return False 10 11 if __name__ == '__main__': 12 yy = raw_input("请输入年份:") 13 if Leapyear(yy) == True: 14 print '闰年' 15 else: 16 print '平年'
5-5.硬币找零,初期的贪心算法(继续用字典)
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 def change(a): 5 aDist = {25:0,10:0,5:0,1:0} 6 b = int(float(a) * 100) 7 for key in aDist: 8 aDist[key] = b / key 9 b %= key 10 sum = 0 11 for key in aDist: 12 sum += aDist[key] 13 return sum 14 15 if __name__ == '__main__': 16 f = raw_input("请输入金额:") 17 print change(f)
5-6.算术问题:主要麻烦在字符串的处理上面,我初次尝试一下使用正则来解决,但是过程颇恶心。
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 import re 5 6 def calculator(Str): 7 pat1 = r'[0-9.]+' #匹配数字 8 pat2 = r'[\+\-\*/%]+' #匹配算术符号 9 aList1 = re.findall(pat1,Str) 10 aList2 = re.findall(pat2,Str) 11 if aList2[0] == '+': 12 return float(aList1[0]) + float(aList1[1]) 13 if aList2[0] == '-': 14 return float(aList1[0]) - float(aList1[1]) 15 if aList2[0] == '*': 16 return float(aList1[0]) * float(aList1[1]) 17 if aList2[0] == '/': 18 return float(aList1[0]) / float(aList1[1]) 19 if aList2[0] == '%': 20 return float(aList1[0]) % float(aList1[1]) 21 if aList2[0] == '**': 22 return float(aList1[0]) ** float(aList1[1]) 23 24 if __name__ == '__main__': 25 ss = raw_input("请输入算式:") 26 print calculator(ss)
5-8.几何问题,本来不想做的,试一下用常量Pi
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 from math import pi 5 6 def square(a): 7 return float(a) ** 2 8 9 def cube(a): 10 return float(a) ** 3 11 12 def circle(r): 13 return pi * float(r) ** 2 14 15 def spherome(r): 16 return 4.0 / 3 * pi * float(r) ** 3 17 18 if __name__ == '__main__': 19 a1 = raw_input("请输入正方形的边长:") 20 print '正方形面积为:',square(a1) 21 a2 = raw_input("请输入立方体的边长:") 22 print '立方体的体积为:',cube(a2) 23 r1 = raw_input("请输入圆的半径:") 24 print '圆的面积为:',circle(r1) 25 r2 = raw_input("请输入球体的半径:") 26 print '球体的体积为:',spherome(r2)
5-10.转换。老题目,摄氏温度转化为华氏温度,题目说什么要用到现实生活的除法(其实python2.2之后的版本'/'已经是真正的除法了),只好做一下呗
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 from __future__ import division 5 6 def change(F): 7 C = (float(F) -32) * 5.0 / 9 8 return C 9 10 if __name__ == '__main__': 11 f = raw_input("摄氏温度为:") 12 print '华氏温度为:%.2f' % change(f)
5-12.系统限制。写一段脚本确认一下你的Python所能处理的整型,长整型,浮点型和复数的范围。这道题真的没思路。后来百度看用到sys模块
1 >>>import sys; #加载sys模块 2 3 >>>print (sys.maxint); 4 >>>print(-sys.maxint-1); 5 6 >>>print(sys.float_info); 7 >>>print(sys.long_info); 8 #最后我这个运行起来有问题不知道为什么,有人说是版本问题
5-16.繁琐的题目,这是我写的。
1 #!/usr/bin/env python 2 #-*-coding=utf-*- 3 4 def Payment(balance,monthly): 5 count = 1 6 print ' Amount Remaining' 7 print 'Pymt# Paid Balance' 8 print '----- ------ -----------' 9 b = float(balance) 10 m = float(monthly) 11 print ' 0 $ 0.00 $%.2f' % b 12 while b >= 0: 13 if b < m: 14 print '%2d $%4.2f $0.00' % (count,b) 15 break 16 else: 17 b -= m 18 print '%2d $%4.2f $%4.2f' % (count,m,b) 19 count += 1 20 21 if __name__ == '__main__': 22 ob = raw_input('Enter opening balance:') 23 mp = raw_input('Enter monthly payment:') 24 Payment(ob,mp)
后来我在网上看到了一种代码行数更简单的方法,跪了。
1 #!/usr/bin/env python 2 #-*-coding=utf-*- 3 4 def Payment(balance,monthly): 5 count = 1 6 print ' Amount Remaining' 7 print 'Pymt# Paid Balance' 8 print '----- ------ -----------' 9 b = float(balance) 10 m = float(monthly) 11 print '%4d%15.2f%15.2f' % (0, 0.00, 100) 12 while (b - count * m ) >= 0: 13 count = count + 1 14 print '%4d%15.2f%15.2f' % (count - 1, m, (b - m*(count - 1))) 15 print '%4d%15.2f%15.2f' % (count, (b - m*(count - 1)), 0) 16 17 if __name__ == '__main__': 18 ob = raw_input('Enter opening balance:') 19 mp = raw_input('Enter monthly payment:') 20 Payment(ob,mp)
5-17.随机数。懂得用ramdom模块以及randint函数
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 import random 5 6 N = random.randint(1, 101) 7 8 list = range(N) #注意这种新建列表的方式 9 10 for i in range(N): 11 list[i] = random.randint(-1, 2**31) 12 13 print 'N =',N 14 print '生成的列表:',list 15 list.sort() 16 print '排序过后:',list
多谢!