Day04—homework
纸上得来终觉浅,绝知此事要躬行。
1.五角数
编写程序:
class Wujiaoshu(object):
def __init__(self):
self.count = 0
def main(self):
for n in range(1,101):
self.getPentagonalNumber(n)
def getPentagonalNumber(self,n):
x = int(n*(3*n-1)/2)
self.count += 1
print(x,end = '\t')
if self.count % 10 == 0:
print('\n',end = '')
if __name__ == '__main__':
wujiaoshu = Wujiaoshu()
wujiaoshu.main()
结果为:
1 5 12 22 35 51 70 92 117 145
176 210 247 287 330 376 425 477 532 590
651 715 782 852 925 1001 1080 1162 1247 1335
1426 1520 1617 1717 1820 1926 2035 2147 2262 2380
2501 2625 2752 2882 3015 3151 3290 3432 3577 3725
3876 4030 4187 4347 4510 4676 4845 5017 5192 5370
5551 5735 5922 6112 6305 6501 6700 6902 7107 7315
7526 7740 7957 8177 8400 8626 8855 9087 9322 9560
9801 10045 10292 10542 10795 11051 11310 11572 11837 12105
12376 12650 12927 13207 13490 13776 14065 14357 14652 14950
2.求整数各位数之和
编写程序:
class Qiuhe(object):
def __init__(self):
self.sum = 0
def main(self):
n = int(input('请输入一个数字:'))
print('该数字的各位数之和为:',self.sumDigits(n))
def sumDigits(self,n):
for i in range(len(str(n))):
num = n //10 ** i % 10
self.sum += num
return self.sum
if __name__ == '__main__':
qiuhe = Qiuhe()
qiuhe.main()
结果为:
请输入一个数字:234
该数字的各位数之和为: 9
3.对三个数排序
编写程序:
class Paixu(object):
def __init__(self):
pass
def main(self):
num1,num2,num3 = eval(input("输入三个数字(逗号分隔):"))
print('排序后为:',self.displaySortedNumbers(num1,num2,num3))
def displaySortedNumbers(self,num1,num2,num3):
list = [num1,num2,num3]
a = sorted(list)
return a
if __name__ == '__main__':
paixu = Paixu()
paixu.main()
结果为:
输入三个数字(逗号分隔):3,2.4,5
排序后为: [2.4, 3, 5]
4.计算未来投资值
编写程序:
class Weilai(object):
def __init__(self):
self.x = 0
def main(self):
investmentAmount = int(input("The amount invested:"))
yearlyInterestRate = int(input("Annual interest rate:"))
self.x = self.futureInvestmentValue(investmentAmount,yearlyInterestRate)
print(self.x)
def futureInvestmentValue(self,investmentAmount,yearlyInterestRate):
month = (float(yearlyInterestRate))/12/100
print("year",'\t',"Future Value")
for years in range(1,31):
futurevalue = investmentAmount*((1+month)**(12*years))
print(years,'\t','%.2f'%futurevalue)
if __name__ == '__main__':
weilai = Weilai()
weilai.main()
结果为:
The amount invested:1000
Annual interest rate:9
year Future Value
1 1093.81
2 1196.41
3 1308.65
4 1431.41
5 1565.68
6 1712.55
7 1873.20
8 2048.92
9 2241.12
10 2451.36
11 2681.31
12 2932.84
13 3207.96
14 3508.89
15 3838.04
16 4198.08
17 4591.89
18 5022.64
19 5493.80
20 6009.15
21 6572.85
22 7189.43
23 7863.85
24 8601.53
25 9408.41
26 10290.99
27 11256.35
28 12312.28
29 13467.25
30 14730.58
5.显示字符
编写程序:
class Xianshi(object):
def __init__(self):
self.x = 0
self.count = 0
def main(self):
ch1 = 65
ch2 = 91
self.x = self.printChars(ch1,ch2)
print(self.x)
def printChars(self,ch1,ch2):
for i in range(ch1,ch2):
self.count += 1
print(chr(i),end = " ")
if self.count % 10 == 0:
print("\n")
if __name__ == '__main__':
xianshi = Xianshi()
xianshi.main()
结果为:
A B C D E F G H I J
K L M N O P Q R S T
U V W X Y Z
6.一年的天数
编写程序:
class Tianshu(object):
def __init__(self):
pass
def main(self):
year1 = 2010
year2 = 2021
print(self.numberOfDaysInAYear(year1,year2))
def numberOfDaysInAYear(self,year1,year2):
for i in range(year1,year2):
if i % 4 == 0 and i % 100 != 0 or i % 400 == 0:
print(i,"年有366天,是闰年")
else:
print(i,"年有365天")
if __name__ == '__main__':
tianshu = Tianshu()
tianshu.main()
结果为:
2010 年有365天
2011 年有365天
2012 年有366天,是闰年
2013 年有365天
2014 年有365天
2015 年有365天
2016 年有366天,是闰年
2017 年有365天
2018 年有365天
2019 年有365天
2020 年有366天,是闰年
2010 年有365天
2011 年有365天
2012 年有366天,是闰年
2013 年有365天
2014 年有365天
2015 年有365天
2016 年有366天,是闰年
2017 年有365天
2018 年有365天
2019 年有365天
2020 年有366天,是闰年
7.显示角
编写程序:
class Jihe(object):
def __init__(self):
pass
def main(self):
x1,y1 = eval(input("x1,y1:"))
x2,y2 = eval(input("x2,y2:"))
print(self.distance(x1,y1,x2,y2))
def distance(self,x1,y1,x2,y2):
juli = ((x1-x2)**2+(y1-y2)**2)**0.5
print("两点间距离为:",'%.2f'%juli)
if __name__ == '__main__':
jihe = Jihe()
jihe.main()
结果为:
x1,y1:0,0
x2,y2:2,2
两点间距离为: 2.83
8.梅森素数
编写程序:
class Meisen(object):
def __init__(self):
pass
def main(self):
i = 2
c = []
d = []
print(self.sushu(i,c,d))
def sushu(self,i,c,d):
while i <=31:
j = 2
while j <= i:
if i % j ==0:
if i == j:
c.append(i)
break
j += 1
i += 1
print('P:',c)
for p in c:
d.append(2**p-1)
print('2^P-1:',d)
if __name__ == '__main__':
meisen = Meisen()
meisen.main()
结果为:
P: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
2^P-1: [3, 7, 31, 127, 2047, 8191, 131071, 524287, 8388607, 536870911, 2147483647]
9.当前时间和日期
编写程序:
class Shijian(object):
def __init__(self):
pass
def main(self):
import time
ticks = time.time()*1000
localtime = time.localtime(time.time())
self.xxxx(ticks,localtime)
def xxxx(self,ticks,localtime):
print ("本地时间为 :", localtime)
print ("当前时间毫秒:", ticks)
if __name__ == '__main__':
shijian = Shijian()
shijian.main()
结果为:
本地时间为 : time.struct_time(tm_year=2019, tm_mon=8, tm_mday=19, tm_hour=21, tm_min=42, tm_sec=44, tm_wday=0, tm_yday=231, tm_isdst=0)
当前时间毫秒: 1566222164812.3909
10.掷骰子
编写程序:
import random
class Shijian(object):
def __init__(self):
pass
def main(self):
a1 = random.randint(1,6)
a2 = random.randint(1,6)
a3 = a1+a2
self.xxxx(a1,a2,a3)
def xxxx(self,a1,a2,a3):
if a3==2 or a3==3 or a3==12:
print("你输了")
elif a3==7 or a3==11:
print("你赢了")
else:
for i in range(1,1000):
a4 = random.randint(1,6)
a5 = random.randint(1,6)
a6 = a5+a4
if a6 == 7:
print("你输了")
break
elif a6 == a3:
print("你赢了")
break
if __name__ == '__main__':
shijian = Shijian()
shijian.main()
结果为:
你赢了