当前位置:   article > 正文

CH4 - Python开发技术—流程控制之分支结构 (头歌)_头歌第3关:约瑟夫环问题

头歌第3关:约瑟夫环问题

目录

 第1关:英制单位英寸与公制单位厘米互换

第2关:百分制成绩转换为等级制成绩

第3关:约瑟夫环问题

 第1关:英制单位英寸与公制单位厘米互换

  1. """
  2. 英制单位英寸和公制单位厘米互换
  3. """
  4. def cmin(value,unit):
  5. ''':param value:长度,
  6. :param unit:单位'''
  7. # 请在此处添加代码 #
  8. # *************begin************#
  9. if unit == 'cm' or unit =='厘米': #两个表示方法
  10. result1 = value/2.54
  11. print ('{:.2f}英寸'.format(result1)) #format使用
  12. elif unit == 'in' or unit =='英寸':
  13. result2 = 2.54*value
  14. print ('{:.2f}厘米'.format(result2))
  15. else:
  16. print('请输入有效的单位')
  17. # **************end*************#
  18. value = input()#输入数字
  19. value = int(value)
  20. unit = input()#输入单位
  21. cmin(value,unit)

第2关:百分制成绩转换为等级制成绩

  1. def invert(score):
  2. '''
  3. 百分制成绩转换为等级制成绩
  4. :param score:百分制分数
  5. :return: 等级(A,B,C,D,E)
  6. '''
  7. # 请在此处添加代码 #
  8. # *************begin************#
  9. if(90<=score<=100): #注意:和“”
  10. return "A"
  11. elif(80<=score<=90):
  12. return "B"
  13. elif(70<=score<=80):
  14. return "C"
  15. elif(60<=score<=70):
  16. return "D"
  17. else: #else后面没有括号
  18. return "E"
  19. # **************end*************#
  20. score = float(input())
  21. grad = invert(score)
  22. print(grad)

第3关:约瑟夫环问题

  1. '''《幸运的基督徒》
  2. 有15个基督徒和15个非基督徒在海上遇险,
  3. 为了能让一部分人活下来不得不将其中15个人扔到海里面去,
  4. 有个人想了个办法就是大家围成一个圈,由某个人开始从1报数,
  5. 报到9的人就扔到海里面,他后面的人接着从1开始报数,
  6. 报到9的人继续扔到海里面,直到扔掉15个人。由于上帝的保佑,
  7. 15个基督徒都幸免于难,问这些人最开始是怎么站的,哪些位置是基督徒哪些位置是非基督徒。
  8. '''
  9. def main():
  10. persons = [True] * 30 #建立列表,30个人围一个圈
  11. counter, index, number = 0, 0, 0
  12. while counter < 15: #
  13. if persons[index]:
  14. number += 1
  15. if number == 9:
  16. persons[index] = False #9号消灭
  17. counter += 1
  18. number = 0
  19. index += 1
  20. index %= 30
  21. for person in persons:
  22. print(1 if person else 0, end='')
  23. if __name__ == '__main__':
  24. main()

约瑟夫环问题大佬详解

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/774235
推荐阅读
相关标签
  

闽ICP备14008679号