# 约瑟夫环问题
def main(): """ 约瑟夫环问题, :return: 基督徒和非基督徒站的顺序。 """ persons = [True] * 30 # 30个人活着,用True表示。 counter = 0 # 死的人。 index = 0 # 下标 number = 0 # 报数 while counter < 15: # 死的人小于15继续进行 if persons[index]: # 活着的人报数 number += 1 if number == 9: # 报数为9的人被杀死 persons[index] = False counter += 1 # 死的人加一 number = 0 # 重新报数 index += 1 # 下标一直增加, index %= 30 # 超过30开始循环,又回到0开始, for person in persons: # 在这些人中,死了的标记为非,没死的标记为基 print(‘基‘ if person else ‘非‘, end=‘‘) if __name__ == ‘__main__‘: main()
# list 的多重使用, 矩阵,棋盘。*****
def main(): names = [‘关羽‘, ‘张飞‘, ‘赵云‘, ‘马超‘, ‘貂蝉‘] subjects = [‘语文‘, ‘数学‘, ‘Python‘] table = [[0] * len(subjects) for _ in range(len(names))] # 创建一个5行3列的矩阵。 for row, name in enumerate(names): print(‘请输入%s的成绩:‘ % name) for col, subject in enumerate(subjects): score = int(input(‘%s:‘ % subject)) # 输入每个位置的值 table[row][col] = score # 将每个位置的值,填进去 print(table) if __name__ == ‘__main__‘: main()
# 说年月日,判断是这一年的第几天
def is_leap_year(y): """ 判断是不是闰年, :param y: 输入年份 :return: 闰年返回True, 否则返回False """ if y % 4 == 0 and y % 100 != 0 or y % 400 == 0: return True return False def which_day(year, month, date): """计算传入的日期是这一年的第几天""" days_of_month = [ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ][is_leap_year(year)] # 调用上面的判断,True返回1, False返回0,以此来选择使用哪个列表。 total = 0 for index in range(month - 1): # 对于输入月之前的每个月,计算天数 total += days_of_month[index] return total + date # 加上当月的天数,就是这一年的第几天了。 if __name__ == ‘__main__‘: print(which_day(2018,3,8))
# 杨辉三角
def triangles(): a = [1] while True: yield a a = [sum(i) for i in zip([0]+a, a+[0])] if __name__ == ‘__main__‘: n = 0 for t in triangles(): print(t) n = n+1 if n == 10: break
# 双色球选注
from random import randrange, randint def display(balls): """ 美化输出球的页面 :param balls: 输入的列表 :return: 优化过的列表 """ for index, ball in enumerate(balls): if index == len(balls) - 1: print(‘|‘, end=‘ ‘) print(‘%02d‘ % ball, end=‘ ‘) print() def random_select(): """ 6个红球的随机数(1-33)和1个蓝球的随机数(1-16) :return: 6个红球和一个蓝球的随机组合 """ red_balls = list(range(1, 34)) select_balls = [] for _ in range(6): index = randrange(len(red_balls)) select_balls.append(red_balls[index]) del red_balls[index] select_balls.sort() select_balls.append(randint(1, 16)) return select_balls def main(): """ 注数,及返回值 :return: 多少注的双色球随机数 """ n = int(input(‘机选几注:‘)) for _ in range(n): display(random_select()) if __name__ == ‘__main__‘: main()
# 时钟
from time import sleep import time from text import timerLocal class Timer(object): def __init__(self, hour=0, minute=0, second=0): self.hour = hour self.minute = minute self.second = second def run(self): self.second += 1 if self.second == 60: self.second = 0 self.minute +=1 if self.minute == 60: self.minute = 0 self.hour += 1 if self.hour == 24: self.hour = 0 def appear(self): return ‘%02d:%02d:%02d‘ % (self.hour, self.minute, self.second) def clock(): time1 = Timer() time1.hour = int(time.strftime(‘%H‘, time.localtime())) time1.minute = int(time.strftime(‘%M‘, time.localtime())) time1.second = int(time.strftime(‘%S‘, time.localtime())) while True: print(time1.appear()) sleep(1) time1.run() if __name__ == ‘__main__‘: clock()
# 分钟倒计时器
from time import sleep from time import time class Clock(object): def __init__(self, hour, minute, second): self.hour = hour self.minute = minute self.second = second def run(self): self.second -= 1 if self.second == 0: if self.minute > 0: self.minute -= 1 self.second = 60 elif self.minute == 0: print(‘时间到!‘) return False return True def appear(self): print(‘%02d:%02d:%02d‘ % (self.hour, self.minute, self.second)) def main(): clock = Clock(0, 0, 10) is_go_on = True while is_go_on: clock.appear() sleep(1) is_go_on = clock.run() if __name__ == ‘__main__‘: main()
# 猜拳
from random import randint, randrange class Guess_number(object): def __init__(self, answer): self.answer = answer def compare(self, my_number): if my_number > self.answer: print(‘小一点!‘) return True if my_number < self.answer: print(‘大一点!‘) return True if my_number == self.answer: print(‘恭喜你猜对了!‘) return False def main(): answer = randrange(int(input(‘请输入你要猜的范围:‘))) is_go_on = True while is_go_on: me = Guess_number(answer) my_number = int(input(‘请输入你要猜的数:‘)) is_go_on = me.compare(my_number) if __name__ == ‘__main__‘: main()
# # 描述平面上的一个点,方法:移动这个点,1.移动到某个地方,2.移动多少 3. 移动了多少距离。
from math import sqrt class Point(object): def __init__(self, x=0, y=0): self._x = x self._y = y def move_by(self, dx, dy): self._x += dx self._y += dy return self._x, self._y def move_to(self, x, y): self._x = x self._y = y return self._x, self._y def distance_to(self, other): dx = self._x - other._x dy = self._x - other._y return sqrt(dx**2 + dy**2) def main(): point = Point() P1 = Point(5, 6) print(point.move_by(1, 1)) print(point.move_to(2, 4)) print(point.distance_to(P1)) if __name__ == ‘__main__‘: main()
# 取本地时间
import time from time import sleep def timerLocal(): while True: sleep(1) minute = time.strftime(‘%M‘, time.localtime()) second = time.strftime(‘%S‘, time.localtime()) hour = time.strftime(‘%H‘, time.localtime()) print(‘%s:%s:%s‘ %(hour, minute, second)) if __name__ == ‘__main__‘: timerLocal()
原文地址:https://www.cnblogs.com/zl666/p/8538312.html
时间: 2024-11-06 07:29:54