1.编程:新键一个LeapYear类,分析后实现判断闰年
class LeapYear():
def __init__(self, year):
self.name = year
def if_leapyear(self):
if self.year % 4 == 0 and self.year % 100 != 0 or self.year % 400 == 0:
return 1
else:
return 0
if __name__=='__name__':
year = int(input('请输入年份'))
y = LeapYear(year)
if y.if_leapyear() == 1:
print("润")
else:
print("平")
2.新建一个BinarySeach类,实现整数列表的二分法查找
import random
class BinarySearch():
def __init__(self, list1):
self.list1 = list1
def sele_BinarySearch(self):
i = 0
j = 10 - 1
while i <= j:
mid = (i + j) // 2
if self.list1[mid] == n:
return mid
else:
if n < self.list1[mid]:
j=min-1
else:
i=min+1
if i>j:
return "未找到"
if __name__=='__name__':
list1=random.sample(range(0,20),10)
list1.sort()
print(list1)
n=int(input("请输入查找的元素值:"))
s1=BinarySearch(list1)
print(s1.sele_BinarySearch())