当前位置:网站首页>妹妹们坐船头,哥哥们岸上走
妹妹们坐船头,哥哥们岸上走
2022-06-09 16:51:00 【算法与编程之美】
0 引言
你是不是还在为“当我们排成一个队列需要每隔几人找出一个人来”而烦恼,在生活中我们想要解决这类问题的话,过程是比较繁琐的,那如果我们使用Python程序直接找出需要的人的编号,是不是事半功倍呢?
1 问题
30个人在一条船上,超载,需要15人下船。于是人们排成一队,排队的位置即为他们的编号。报数,从1开始,数到9的人下船。如此循环,直到船上仅剩15人为止,问都有哪些编号的人下船了呢?
2 方法
此题的实际意义是指当船上的人大于15时就让第九个人下船,直到船上只有15个人即可。因此我们可以先将船上的30人存入列表people中,然后定义一个空白列表down。使用while循环结构,当people列表的元素大于15时即len(people)>15,将列表中的第8个元素people[8]添加到dowm列表中,然后将前8个people[:8]放在people[9:]后面(此处使用切片),继续寻找下一个第9人,最后返回列表down。
3 实验结果与讨论
通过实验、实践等证明提出的方法是有效的,是能够解决开头提出的问题。
代码清单 1
| def f(): people = list(range(1,31)) down = [] while len(people)>15: down.append(people[8]) people = people[9:] + people[:8] return down print(f()) |
4 结语
针对下船问题,提出类似环形链表构造数据的方法,通过实际大量实验,证明该方法是有效的,在实验中,我们常常会思考为什么会需要解决这个问题,实际意义是什么,但其实在学习的过程中,学到的不仅仅是这个代码,更多学到的是怎么去解决问题,也是实际工作素养的提升和有效的锻炼。因此,在学习过程中,更应该注重思考。
边栏推荐
- Swagger login address (error may be reported when logging in for the first time)
- How about opening an account with tongdaxin? Is it safe to open an account?
- 性能优化方案
- 面试题—华为机试之知识点补充
- 关联数组&正则表达式
- 中原银行统一日志平台
- It's too voluminous. A company has completely opened the core system it has operated for many years
- Interview questions - Supplement of Huawei computer test knowledge points
- 【華東師範大學】初試複試考研資料分享
- La seconde guerre mondiale a été piégée dans ces 4 points d'erreur, un an de plus pour rien!
猜你喜欢

ArcGIS Pro优化方法

Having written code in Huawei for more than ten years, a programmer's self-cultivation

如何训练你的准确率?

运行代码,想加个进度条实时看以下代码运行进度,怎么破?

Leetcode 1967. 作为子字符串出现在单词中的字符串数目

大数据生态安全框架的实现原理与最佳实践(上篇)

图解 Google V8 # 06:原型链:V8是如何实现对象继承的?

Détails des boutons de la barre d'outils de l'interface utilisateur de l'outil de test automatique d'autorunner - tous les tests | Ze Zhong Cloud test

【華東師範大學】初試複試考研資料分享

About not adding mysql data. DLL
随机推荐
CPU program interrupt
ArcGIS Pro优化方法
Apple wins us judge dismisses iPhone and iPad security defect class action
永远并不远,五种可能注定要衰落的编程语言
Sphereex officially open source the database mesh oriented solution pisanix
中原银行统一日志平台
面试题—华为机试之知识点补充
为什么 SQL 语句使用了索引,但却还是慢查询?
idea卸载教程
Word tutorial, how to change line spacing in word?
关联数组&正则表达式
GTF/GFF文件的差异及其相互转换(转载)
pyepics --Auto-saving: simple save/restore PVs
顶级模板大全(包含工作各方面的合同、标书、毕业答辩等等模板)
Cloud security daily 220609: Cisco cloud application policy infrastructure controller found privilege upgrade vulnerability, which needs to be upgraded as soon as possible
使用tesseract识别图片中的文字
Interview questions - Supplement of Huawei computer test knowledge points
Construction of sheep (rare species) bsgenome reference genome
Question d: Ma Zori
SphereEx 正式开源面向 Database Mesh 的解决方案 Pisanix