当前位置: 首页> 汽车> 车展 > 制作只有一张图片的网站_前端项目_外包公司的人好跳槽吗_优化大师windows

制作只有一张图片的网站_前端项目_外包公司的人好跳槽吗_优化大师windows

时间:2025/7/12 19:43:53来源:https://blog.csdn.net/m0_45426637/article/details/146963743 浏览次数: 0次
制作只有一张图片的网站_前端项目_外包公司的人好跳槽吗_优化大师windows

目录标题

      • 约瑟夫环
      • 学生选课表

约瑟夫环

  • 问题描述:m个人围成一圈,从其中任意一个人开始,按顺时针顺序所有人依次从1开始报数,报到n的人出列;然后n后面的第一个人接着从1开始报数,报到n的人出列…如此下去,直到所有人出列。

  • m和n为具体数字,自由设定。

  • 示意图:m=8,n=3。
    在这里插入图片描述

  • 代码实现

class Node:def __init__(self, data):self.data = dataself.next = None
class Josephus:def __init__(self):# 创建结点self.head = Node(None)self.head.next = self.headdef isEmpty(self):return self.head.next is self.headdef display(self):if self.isEmpty():print('循环链表为空')returnprint('约瑟夫环元素为:', end='')cur = self.head.nextwhile cur != self.head:print(cur.data, end=' ')cur = cur.nextprint()def append(self, data):"""插入操作:param data: 待插入元素:return:"""rear = self.headwhile rear.next != self.head:rear = rear.nextnewNode = Node(data)rear.next = newNodenewNode.next = self.headdef joseph(self, n):"""约瑟夫环核心算法:param n: 一个循环的数值:return:"""if self.isEmpty():print('约瑟夫环为空环!')returnprint('约瑟夫环的出列顺序为:', end='')node = self.headwhile node.next != node:pre = nodefor i in range(n-1):pre = pre.nextnode = pre.nextif(node == self.head):node = node.nextprint(node.data, end=' ')pre.next = node.nextif __name__ == '__main__':print('PyCharm')# 约瑟夫环调试# m:表示有m个人m = 8# n:表示经过n个位置出列一个人n = 3# 初始化约瑟夫环josephus = Josephus()for i in range(m):josephus.append(i + 1)josephus.display()# 执行约瑟夫环核心算法josephus.joseph(n)

学生选课表

  • 顺序表存储结构定义学生选课表
  • 学生选课表示意图
    在这里插入图片描述
  • 代码实现
# 学生选课表
class StudentCourse:def __init__(self, seq, no, course, teacher, score):self.seq = seq  # 序号self.no = no  # 学号self.course = course  # 课程名self.teacher = teacher  # 教师self.score = score  # 成绩class CourseList:def __init__(self):self.students = []  # 存储学生对象的列表self.last = -1  # 当前最后一个元素的索引def isEmpty(self):return self.last == -1def getLength(self):return self.last + 1def display(self):if self.isEmpty():print("当前顺序表为空!")returnprint('\n当前学生选课表:')print(f"{'序号':<6}{'学号':<8}{'课程名':<10}{'教师':<8}{'成绩':<6}")print('-' * 40)for student in self.students:print(f"{student.seq:<8}{student.no:<10}{student.course:<12}{student.teacher:<10}{student.score:<6}")def insertCourse(self, pos, student):if pos < 0 or pos > self.getLength():print("错误:插入位置越界")return False# 插入元素并更新索引self.students.insert(pos, student)self.last += 1print(f"成功插入 {student.no}{student.course} 课程")return Truedef searchByNo(self, no):results = [s for s in self.students if s.no == no]if not results:print(f"未找到学号 {no} 的选课记录")return Noneprint(f"\n学号 {no} 的选课记录:")for s in results:print(f"课程: {s.course}, 教师: {s.teacher}, 成绩: {s.score}")return resultsdef deleteByNo(self, no):original_len = self.getLength()self.students = [s for s in self.students if s.no != no]self.last = len(self.students) - 1deleted = original_len - self.getLength()print(f"删除学号 {no}{deleted} 条记录")def show_menu():print("\n学生选课管理系统")print("1. 显示所有记录")print("2. 插入新记录")print("3. 按学号查询")print("4. 按学号删除")print("5. 退出系统")return input("请输入操作编号:")if __name__ == '__main__':print('PyCharm')# 学生选课表# 初始化顺序表course = CourseList()# 添加初始测试数据init_data = [StudentCourse(1, "S001", "高等数学", "王老师", 59),StudentCourse(2, "S002", "离散数学", "李老师", 90),StudentCourse(3, "S003", "操作系统", "陈老师", 67),StudentCourse(4, "S004", "数据结构", "罗老师", 89),StudentCourse(5, "S005", "机器学习", "方老师", 78)]for idx, student in enumerate(init_data):course.insertCourse(idx, student)while True:choice = show_menu()if choice == '1':  # 显示记录course.display()elif choice == '2':  # 插入记录try:print("\n插入新记录(输入exit返回菜单)")seq = int(course.getLength()+1)no = input("学号:").strip()cor = input("课程名:").strip()teacher = input("教师:").strip()score = int(input("成绩:"))pos = int(input("插入位置(0-{}):".format(course.getLength())))if course.insertCourse(pos, StudentCourse(seq, no, cor, teacher, score)):course.display()except ValueError:print("输入格式错误!")elif choice == '3':  # 按学号查询no = input("\n请输入要查询的学号:").strip()course.searchByNo(no)elif choice == '4':  # 按学号删除no = input("\n请输入要删除的学号:").strip()course.deleteByNo(no)course.display()elif choice == '5' or choice.lower() == 'exit':print("系统已退出")breakelse:print("无效的输入,请重新选择!")
关键字:制作只有一张图片的网站_前端项目_外包公司的人好跳槽吗_优化大师windows

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: