笔记:udacity计算机导论
来源:互联网 发布:网络封包抓取 编辑:程序博客网 时间:2024/05/23 15:42
# Define a procedure, stamps, which takes as its input a positive integer in
# pence and returns the number of 5p, 2p and 1p stamps (p is pence) required# to make up that value. The return value should be a tuple of three numbers
# (that is, your return statement should be followed by the number of 5p,
# the number of 2p, and the nuber of 1p stamps).
#
# Your answer should use as few total stamps as possible by first using as
# many 5p stamps as possible, then 2 pence stamps and finally 1p stamps as
# needed to make up the total.
#
# (No fair for USians to just say use a "Forever" stamp and be done with it!)
# 这个题求一定金额的邮资需要多少张5p、2p、1p的邮票组成,要求邮票张数最少。巧用余数,其实该题很简单
def stamps(a):
s5 = a/5
s2 = a % 5/2
s1 = a % 5 % 2
return (s5,s2,s1)
# Your code here
print stamps(8)
#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps
上面的代码一大值得注意的是,要求输出的是元组。
虽然可以用字符相连,输出看起来相似的答案,但因为结果的类型不同,因此是这种做法是错误的
a = "("+ str(s5) + "," + str(s2) + "," + str(s1) + ")"
print type(a)
b = (s5,s2,s1)
print type(b)
阅读全文
0 0
- 笔记:udacity计算机导论
- 笔记:udacity计算机导论
- 笔记:udacity计算机导论
- 笔记:udacity计算机导论
- 笔记:udacity计算机导论
- 笔记:udacity计算机导论
- 笔记:udacity计算机导论
- 难点笔记:udacity计算机导论
- 难点笔记:udacity计算机导论
- 难点笔记:udacity计算机导论
- 难点笔记:udacity计算机导论- 37使用数字查找
- 笔记:udacity计算机导论 -5-29/30 查找、提取、并输出超链 if vs while
- Udacity笔记
- 计算机导论第三课笔记
- 笔记:udacity计算机科学导论- 课程11/12-14 数组值的改变和增加
- 笔记:udacity计算机科学导论- 课程11/14-17数组append,+,len的谜题
- 笔记:udacity计算机科学导论- 课程11/19 python取幂, DRAM 存储原理、容量、速度
- 笔记:udacity计算机科学导论- 课程11/21 各种存储方式比较
- QT常见的窗口部件
- JavaScript
- 普及练习场 排序 宇宙总统
- JAVA设计模式之单例模式
- 定义类的数组 及其使用
- 笔记:udacity计算机导论
- poj--2485--Highways
- 23种设计模式汇总整理
- AutoCAD .Net 创建Ribbon界面(一)
- Python——网络爬虫(爬取网页图片)
- 阿里云服务器安装jdk、tomcat、mysql
- Idea 创建WebService服务端和客户端
- 大数据学习笔记(十)-Hive中的Storage format
- 使用git reset 合并只有两个commit到一个