16.一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. 程序分析:请参照程序Python 100例中的第14个例子 #python3.7 from sys import stdout for j in range(2, 1001): k = [] n = -1 s = j for i in range(1, j): if j % i == 0: n += 1 s -= i k.append(i) if s == 0: print…
55.学习使用按位取反~. 程序分析:~0=1; ~1=0; (1)先使a右移4位. (2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) (3)将上面二者进行&运算. #python3.7 if __name__ == '__main__': a = 234 b = ~a print('The a\'s 1 complement is %d' % b) a = ~a print('The a\'s 2 complement is %d' % a) 56.画图,学用circ…
13.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方. 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位. #python3.7 import math for n in range(100,1000): i = math.floor(n / 100)#百位数 j = math.floor((n -…
73.反向输出一个链表. #python3.7 if __name__ == '__main__': ptr = [] for i in range(5): num = int(input('Please input a number:\n')) ptr.append(num) print(ptr) ptr.reverse() print(ptr) 74.列表排序及连接. #python3.7 if __name__ == '__main__': a = [1, 2, 3] b = [3, 4,…
67.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组. #python3.7 def inp(numbers): for i in range(6): numbers.append(int(input('输入一个数字:\n'))) p = 0 def arr_max(array): max = 0 for i in range(1, len(array) - 1): p = 1 if array[p] > array[max] : max = p k = max array…
61.打印出杨辉三角形. #python3.7 from sys import stdout if __name__ == '__main__': a = [] for i in range(10): a.append([]) for j in range(10): a[i].append(0) for i in range(10): a[i][0] = 1 a[i][i] = 1 for i in range(2,10): for j in range(1,i): a[i][j] = a[i…
49.使用lambda来创建匿名函数. #python3.7 MAXIMUM = lambda x,y : (x > y) * x + (x < y) * y MINIMUM = lambda x,y : (x > y) * y + (x < y) * x if __name__ == '__main__': a = 10 b = 20 print('The larger one is %d' % MAXIMUM(a, b)) print('The lower one is %d'…