# -*- coding: utf-8 -*-

# 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
# 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
# 程序源代码:

def permutation(nums):
    for x in nums:
        for y in nums:
            for z in nums:
                if len(set({x, y, z})) == 3:
                    print '{}{}{}'.format(x, y, z)

if __name__ == '__main__':
    permutation(range(1, 5))

 

解释:if __name__ == '__main__':

只有当这个文件在命令行被直接执行时,才运行下面的代码。

python标准建议 使用 format, 不建议用 %

print '{}{}{}'.format(x, y, z) 格式化字符串

或者是这个样子

 '{x}{y}{x}'.format(x=5, y='x')

Python 100道题深入理解

 

相关文章:

  • 2021-09-13
  • 2022-12-23
  • 2021-07-18
  • 2021-11-23
  • 2021-11-21
  • 2021-08-10
猜你喜欢
  • 2021-11-22
  • 2022-12-23
  • 2022-02-21
  • 2021-05-19
  • 2021-12-03
  • 2022-01-07
  • 2022-01-25
相关资源
相似解决方案