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[0], array[k] = array[k], array[0] def arr_min(array): min = 0 for i in range(1, len(array) - 1): p = 1 if array[p] < array[min] : min = p l = min array[5], array[l] = array[l], array[5] def outp(numbers): for i in range(len(numbers)): print(numbers[i]) if __name__ == '__main__': array = [] inp(array) arr_max(array) arr_min(array) print('计算结果:') outp(array)
68、有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。
#python3.7 if __name__ == '__main__': n = int(input('整数n为:\n')) m = int(input('向后移m个位置为:\n')) def move(array, n, m): array_end = array[n -1] for i in range(n - 1, -1, -1): array[i] = array[i - 1] array[0] = array_end m -= 1 if m > 0: move(array, n, m) number = [] for i in range(n): number.append(int(input('输入一个数字:\n'))) print('原始列表:', number) move(number, n, m) print('移动之后:', number)
69、有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
#python3.7 if __name__ == '__main__': nmax = 50 n = int(input('请输入总人数:')) num = [] for i in range(n): num.append(i + 1) i = 0 k = 0 m = 0 while m < n - 1: if num[i] != 0 : k += 1 if k == 3: num[i] = 0 k = 0 m += 1 i += 1 if i == n : i = 0 i = 0 while num[i] == 0 : i += 1 print(num[i])
70、写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
#python3.7 if __name__ == '__main__': s = input('Please input a string:\n') print('The string has %d characters.' % len(s))
71、编写input()和output()函数输入,输出5个学生的数据记录。
N = 3 #stu # num : string # name : string # score[4]: list student = [] for i in range(5): student.append(['','',[]]) def input_stu(stu): for i in range(N): stu[i][0] = raw_input('input student num:\n') stu[i][1] = raw_input('input student name:\n') for j in range(3): stu[i][2].append(int(raw_input('score:\n'))) def output_stu(stu): for i in range(N): print('%-6s%-10s' % ( stu[i][0],stu[i][1] )) for j in range(3): print('%-8d' % stu[i][2][j]) if __name__ == '__main__': input_stu(student) print(student) output_stu(student)
72、创建一个链表。
#python3.7 if __name__ == '__main__': ptr = [] for i in range(5): num = int(input('Please input a number:\n')) ptr.append(num) print(ptr)
参考资料:
Python 100例