python学习——练习题(1)
"""
题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
"""
import itertools def answer1():
"""自己思考完成,一开始以为两个循环就可以搞定了,结果还是要用三个循环;打印时只要效果在就好了,不用专门用int去组合成三位数字了"""
print("答案一", end=":")
x = ("1", "2", "3", "4")
r = range(0, 4)
n = 0
for i in r:
for j in r:
if i != j:
for k in r:
if k != i and k != j:
s = "%s%s%s" % (x[i], x[j], x[k])
n += 1
print(s, end=',')
print("共%d个" % n) answer1() def answer2():
"""参考答案,本以为自己用字符串组合,够简洁了,但参考答案直接输出个样子就得了"""
print("答案二", end=":")
r = range(1, 5)
n = 0
for i in r:
for j in r:
for k in r:
if i != j and i != k and j != k:
n += 1
print("%d%d%d" % (i, j, k), end=',')
print("共%d个" % n) answer2() def answer3():
"""利用列表来计算总数"""
print("答案三", end=":")
d = []
r = range(1, 5)
for a in r:
for b in r:
for c in r:
if a != b and a != c and b != c:
d.append("%d%d%d" % (a, b, c))
print(d, end=",")
print("共%d个" % len(d)) answer3() def answer4():
"""在列表里面使用for 和 if 操作可迭代数据"""
print("答案四", end=":")
arr = [1, 2, 3, 4]
newList = ["%d%d%d" % (i, j, k)for i in arr for j in arr for k in arr if i != j and i != k and j != k]
print(newList, end=",")
print("共%d个" % len(newList)) answer4() def answer5():
"""使用从最小值到最大值轮训查找, 注意在求十位上的数值时,先求模后整除"""
print("答案五", end=":")
r = range(123, 433)
n = 0
for i in r:
a = i % 10
b = (i % 100) // 10
c = i // 100
if a != b and a != c and b != c and 0 < a < 5 and 0 < b < 5 and 0 < c < 5:
n += 1
print(i, end=",")
print("共%d个" % n) answer5() def answer6():
"""使用集合的自动去重功能来生成三位数的组合"""
print("答案六", end=":")
r = range(1, 5)
n = 0
for i in r:
for j in r:
for k in r:
if len(set((i, j, k))) == 3:
n += 1
print("%d%d%d" % (i, j, k), end=",")
print("共%d个" % n) answer6() def answer7():
"""
利用python自带的排列方法,可以获取到所有的结果
combinations方法重点在组合,permutations方法重在排列。
还有就是,combinations和permutations返回的是对象地址,
原因是在python3里面,返回值已经不再是list,而是iterators(迭代器),
所以想要使用,只用将iterator 转换成list 即可, 还有其他一些函数返回的也是一个对象,需要list转换,比如 list(map())等
"""
print("答案七", end=":")
s = "1234"
resultList = ["".join(i) for i in list(itertools.permutations(s, 3))]
print(resultList, end=",")
print("共%d个" % len(resultList)) answer7() def answer8():
"""利用列表的删除机制来实现"""
print("答案八", end=":")
listNum = [1, 2, 3, 4]
n = 0
for i in listNum:
listNum1 = listNum.copy()
listNum1.remove(i)
for j in listNum1:
listNum2 = listNum1.copy()
listNum2.remove(j)
for k in listNum2:
n += 1
print("%d%d%d" % (i, j, k), end=",")
print("共%d个" % n) answer8() def answer9():
"""
利用位运算来实现,具体原理大概是这样的:
1,2,3,4 只有四个数字, 我们只要用两个二进制位(2bit)就可以表示:00,01,10,11;
而生成的三位数,我们可以用六个二进制位(6bit)来表示,如123可以表示为:00 01 10。
根据题目要求我们可以知道能生成的最大值和最小值是123和432 ,所以我们可以取区间range(123,433),可见answer5
用二进制替换就是range(00 01 10,11 10 10),转为十进制就是range(6,58).
将i右移4位再和3进行与运算,可以获取百位的数值,例如234,可以表示01 10 11(转成十进制为27,在6-58之间),
右移4位得到01,再&3(二进制即11)可将01左边清零,得到想要的01,获取十位上的数值,就只要右移2位即可,&3都是为了左边清零,
异或运算其实就是不等于运算,最后加一是将00,01,10,11 转为对应的1,2,3,4
"""
print("答案九", end=":")
r = range(6, 58)
n = 0
for i in r:
a = i >> 4 & 3
b = i >> 2 & 3
c = i & 3
if a ^ b and b ^ c and c ^ a:
n += 1
print("%d%d%d" % (a+1, b+1, c+1), end=",")
print("共%d个" % n) answer9()
python学习——练习题(1)的更多相关文章
- python学习——练习题(10)
""" 题目:暂停一秒输出,并格式化当前时间. """ import sys import time def answer1(): &quo ...
- python学习——练习题(9)
""" 题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. http://www.runoob.com/python/python-date- ...
- python学习——练习题(6)
""" 题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21 ...
- python学习——练习题(4)
""" 题目:输入某年某月某日,判断这一天是这一年的第几天? """ import datetime import time from fu ...
- python学习——练习题(13)
""" 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个" ...
- python学习——练习题(12)
""" 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...
- python学习——练习题(11)
""" 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1 1 2 ...
- python学习——练习题(8)
""" 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...
- python学习——练习题(7)
""" 题目:将一个列表的数据复制到另一个列表中. """ import copy def validate(a, b): "&q ...
随机推荐
- Flask 分页的简单用法 / flask_sqlalchemy /无刷新翻转页面(原创)
flask_sqlalchemy对象提供分页方法 1. 后台views代码: from models import <table_name> #导入model的对象 @app.route( ...
- LeetCode OJ:First Bad Version(首个坏版本)
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- PostBack IsPostBack
这涉及到aspx的页面回传机制的基础知识 postback是回传 即页面在首次加载后向服务器提交数据,然后服务器把处理好的数据传递到客户端并显示出来,就叫postback, ispostback只是一 ...
- Django 使用 内置 content-type
django内置的content-type组件, 记录了项目中所有model元数据的表 可以通过一个ContentType表的id和一个具体表中的id找到任何记录,及先通过ContenType表的id ...
- [Scala]Scala学习笔记三 Map与Tuple
1. 构造映射 可以使用如下命令构造一个映射: scala> val scores = Map("Alice" -> 90, "Kim" -> ...
- 你必须知道的495个C语言问题,学习体会三
本文是 本系列的第三篇,本文主要对C语言的表达式做个小结 先从两个坑爹的表达式说起:i++ 与++i 上大学的时候,学长告诉我,这两个表达式,意义是一样的,后来老师纠正说,还是有区别的,于是让我们记住 ...
- 【MFC】SetWindowPos函数使用详解
摘自: http://wenku.baidu.com/link?url=hYKs20rYA13TTdMl9gJ378GNOsxH1DPZPkYZVEIcipATlVBMLzjWdpd2-29fm-tq ...
- rebar安装及创建项目
rebar作为erlang开发中编译,构建,发布,打包,动态升级的常用工具,下面我记录下rebar工具的安装及使用 从源码安装rebar 1. 建立文件 install_rebar.sh 2. 拷贝如 ...
- 解决Net内存泄露原因
Net内存泄露原因及解决办法 https://blog.csdn.net/changtianshuiyue/article/details/52443821 什么是.Net内存泄露 (1).NET 应 ...
- ActionContextCleanUp作用
延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让action自己清除. 为了使用WebWork,我们只需要 ...