博客
关于我
python笔记1-用python解决小学生数学题
阅读量:466 次
发布时间:2019-03-06

本文共 712 字,大约阅读时间需要 2 分钟。

为了找出面值为6角、7角和8角的邮票的最大不可支付邮资,我们可以按照以下步骤进行:

生成所有可能的邮票组合

使用三个邮票面值,每种邮票最多使用50张,计算所有可能的邮资组合。

排序和去重

将所有可能的邮资排序并去重,得到一个连续的邮资范围。

找出最大不可支付邮资

检查从1角开始的邮资是否存在缺口,找出最大的缺口处的邮资。

代码实现

import itertoolsa, b, c = 6, 7, 8t = 50# 生成所有可能的邮票组合combinations = []for counts in itertools.product(range(t + 1), repeat=3):    total = a * counts[0] + b * counts[1] + c * counts[2]    combinations.append(total)# 去重并排序unique = sorted(list(set(combinations)))# 找出最大的不可支付邮资max_paid = unique[-1] if unique else 0max_incap = 0for i in range(1, max_paid + 1):    if i not in unique:        max_incap = i        breakif max_incap == 0:    print("所有邮资都可以支付,最大的不可支付邮资是:0元")else:    print("最大的不可支付邮资是:%s元" % max_incap)

结果

通过上述步骤,我们发现最大的不可支付邮资为17角,即1.7元。

转载地址:http://jkmbz.baihongyu.com/

你可能感兴趣的文章
python3简单使用requests 用户代理,cookie池
查看>>
Python3组播通信编程实现教程(发送者+接收者)
查看>>
Python3编写网络爬虫10-数据存储方式三-CSV文件存储
查看>>
python3读取redis返回数据带有'b'的问题
查看>>
Python3连接MySQL
查看>>
Python关于isinstance函数的解析 | 附Demo(全)
查看>>
Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
查看>>
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>
Python: Windows下用 multiprocessing 的深坑
查看>>
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>