标签:span usr 相同 coding python 1 sum 相互 env python
1 #!/usr/bin/env python 2 # coding:utf-8 3 4 ‘‘‘有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?‘‘‘ 5 n = 1 6 num = [4, 2, 3, 1] 7 list_num = [i * 100 + j * 10 + k for i in num for j in num for k in num if (i != j) and (j != k) and (i != k)] 8 print(‘能组成%s个相互不同且无重复的数字的三位数‘ % len(list_num)) 9 print(‘它们分别是%s‘ % list_num) 10 11 # 按照从小到大排序 12 for i in xrange(len(list_num)): 13 for j in xrange(len(list_num)): 14 if list_num[i] < list_num[j]: 15 list_num[i], list_num[j] = list_num[j], list_num[i] 16 print(‘它们从小到大的顺序是%s‘ % list_num) 17 18 # 求所有满足要求的三位数之和 19 sum = 0 20 for i in list_num: 21 sum = sum + i 22 print(‘它们的累计求和是%s‘ % sum) 23 24 sum_0 = 0 25 sum_1 = 0 26 # 求所有偶数和 27 for i in list_num: 28 if i % 2 == 0: 29 sum_0 = sum_0 + i 30 print(‘所有偶数求和是%s‘ % sum_0) 31 32 # 求所有奇数和 33 for i in list_num: 34 if i % 2 != 0: 35 sum_1 = sum_1 + i 36 print(‘所有偶数求和是%s‘ % sum_1)
标签:span usr 相同 coding python 1 sum 相互 env python
原文地址:https://www.cnblogs.com/calvin1102/p/9219079.html