Python练手例子(5)】的更多相关文章

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 -…
91.时间函数举例1. #!/usr/bin/python #coding=utf-8 import time if __name__ == '__main__': #time.time()返回当前的时间戳(1970纪元后经过的浮点秒数) print(time.time()) #time.ctime()把时间戳转化为time.asctime()的形式 print(time.ctime(time.time())) #time.asctime()返回"Tue Feb 26 09:12:37 2019…
85.输入一个奇数,然后判断最少几个 9 除于该数的结果为整数. 程序分析:999999 / 13 = 76923. #!/usr/bin/python #coding=utf-8 if __name__ == '__main__': zi = int(input('输入一个数字:\n')) n1 = 1 c9 = 1 m9 = 9 sum = 9 while n1 != 0: if sum % zi == 0: n1 = 0 else: m9 *= 10 sum += m9 c9 += 1 p…
79.字符串排序. #python3.7 if __name__ == '__main__': str1 = input('Input string:\n') str2 = input('Input string:\n') str3 = input('Input string:\n') print(str1, str2, str3) if str1 > str2 : str1, str2 = str2, str1 if str1 > str3 : str1, str3 = str3, str1…
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'…