第4章-1 生成3的乘方表
num = eval(input()) for i in range(num + 1): print("pow(",num,",",i,") = ",pow(num,i),sep = '')
第4章-2 统计素数并求和
M,N = map(int,input().split(" ")) count = 0 sum = 0 if M > 2: for i in range(M,N + 1): for j in range(2,i): if i % j != 0: flag = True continue else: flag = False break if flag: count = count + 1 sum = sum + i elif M == 2: for i in range(M + 1,N + 1): for j in range(2,i): if i % j != 0: flag = True continue else: flag = False break if flag: count = count + 1 sum = sum + i sum = sum + 2 count = count + 1 else: for i in range(M + 2,N + 1): for j in range(2,i): if i % j != 0: flag = True continue else: flag = False break if flag: count = count + 1 sum = sum + i sum = sum + 2 count = count + 1 print(count,sum)
第4章-3 猴子吃桃问题
N = eval(input()) day = 1 peach = 1 while day < N: peach = peach * 2 + 2 day = day + 1 print(peach)
第4章-4 验证“哥德巴赫猜想”
#1不是素数 num = eval(input()) ls = [2] dict = {} for i in range(3,num): for j in range(2,i): if i % j != 0: flag = True continue else: flag = False break if flag: ls.append(i) for k in range(len(ls)): for t in range(k + 1,len(ls)): if(ls[k] + ls[t] == num): dict.update({ls[k]:ls[t]}) new_dict = list(zip(dict.keys(),dict.values())) p = new_dict[0][0] q = new_dict[0][1] print(num," = ",p," + ",q)
第4章-5 求e的近似值
num = eval(input()) sum = 1 def function(n): factor = 1 res = 1 for i in range(1,n + 1): res = res * i return res for j in range(1,num + 1): res = function(j) sum = sum + 1/res print(format(sum,'.8f'))
第4章-6 输出前 n 个Fibonacci数
num = eval(input()) ls = [1,1] cnt = 1 count = 0 while cnt <= num: ls.append(ls[cnt-1] + ls[cnt]) cnt = cnt + 1 if num < 1: print("Invalid") else: for t in range(num): print("{:11d}".format(ls[t]),end = '') count = count + 1 if count % 5 == 0: print("\n")
第4章-7 统计学生平均成绩与及格人数
num = eval(input()) pass_num = 0 sum = 0 score = input().split(" ") for i in range(len(score)): sum = sum + int(score[i]) if int(score[i]) >= 60: pass_num = pass_num + 1 print("average = ",format(sum/num,".1f"),sep = '') print("count = ",pass_num)
第4章-8 求分数序列前N项和
num = eval(input()) numerate = [2] denominator = [1] count = 1 sum = 0 while count <= num: numerate.append(numerate[count - 1] + denominator[count - 1]) denominator.append(numerate[count - 1]) count = count + 1 for i in range(num): sum = sum + numerate[i]/denominator[i] print(format(sum,'.2f'))
第4章-9 查询水果价格
print("[1] apple") print("[2] pear") print("[3] orange") print("[4] grape") print("[0] exit") requests = input().split(" ") ls = [] count = 0 for request in requests: ls.append(request) if len(ls) <= 5: for i in ls: if i == '1': print("price = 3.00") elif i == '2': print("price = 2.50") elif i == '3': print("price = 4.10") elif i == '4': print("price = 10.20") elif i == '0': break else: print("price = 0.00") elif len(ls) > 5: for i in ls: if i == '1': print("price = 3.00") count = count + 1 elif i == '2': print("price = 2.50") count = count + 1 elif i == '3': print("price = 4.10") count = count + 1 elif i == '4': print("price = 10.20") count = count + 1 elif i == '0': count = 0 break else: print("price = 0.00") count = count + 1 if count >= 5: break else: continue
第4章-10 最大公约数和最小公倍数
M,N = map(int,input().split(" ")) #最大公约数 #辗转相除法(欧几里得算法) #算法思想:除数和余数反复做除法运算,当余数为0时,当前算数除数为最大公约数 A = M B = N if M < N: t = N N = M M = t r_gcd = M % N while r_gcd: M = N N = r_gcd r_gcd = M % N gcd = N #最小公倍数 r_lcm = A % B if A < B: t = B B = A A = t if not r_lcm: lcm = A else: i = A + 1 while not (i % B == 0 and i % A == 0): i = i + 1 lcm = i print(gcd,lcm)
第4章-11 判断素数
num = eval(input()) count = 1 ls = [] prime_num = [] while count <= num: t = eval(input()) ls.append(t) count = count + 1 for i in ls: for j in range(2,i): if i % j == 0: flag = False break else: flag = True continue if flag: prime_num.append(i) for k in ls: if k in prime_num: print("Yes") else: print("No")
第4章-12 求满足条件的斐波那契数
num = eval(input()) ls = [1,1] for i in range(2,num): res = ls[i - 1] + ls[i - 2] ls.append(res) for k in ls: if k > num: print(k) break else: continue
第4章-13 求误差小于输入值的e的近似值
error = eval(input()) #定义阶乘求解函数 def factorial(n): res = 1 for i in range(1,n+1): res = res * i return res j = 1 while j >= 1: res1 = 1 / factorial(j) if res1 < error: break else: j = j + 1 continue e = 1 for t in range(1,j + 1): e = e + (1 / factorial(t)) print(format(e,'.6f'))
第4章-14 统计字符
strs = '' enter = 0 letter = 0 blank = 0 digit = 0 other = 0 while True: str = input() strs = strs + str enter = enter + 1 if len(strs) + enter > 10: enter = enter - 1 break for i in strs: if i.isdigit(): digit = digit + 1 elif i.isalpha(): letter = letter + 1 elif i == ' ': blank = blank + 1 else: other = other + 1 print("letter = ",letter,", blank = ",blank + enter,", digit = ",digit,", other = ",other,sep = '')
第4章-15 换硬币
sum = eval(input()) count = 0 for i in range(sum // 5,0,-1): for j in range(sum // 2,0,-1): for k in range(sum // 1,0,-1): if i * 5 + j * 2 + k * 1 == sum: print("fen5:",i,"fen2:",j,"fen1:",k,"total:",i + j + k) count = count + 1 print("count =",count)
第4章-16 jmu-python-判断是否构成三角形
a,b,c = map(int,input().split(" ")) if a + b > c and a + c >b and b + c > a: print("yes") else: print("no")
第4章-17 水仙花数
#水仙花数 N = eval(input()) def function(num,N): ls = [] mask = pow(10,N - 1) while mask >= 1: ls.append(num // mask % 10) mask = mask // 10 return ls if N == 3: for i in range(100,1000): sum = 0 order = function(i,N) for j in order: sum = sum + pow(j,N) if sum == i: print(i) elif N == 4: for i in range(1000,10000): sum = 0 order = function(i, N) for j in order: sum = sum + pow(j,N) if sum == i: print(i) elif N == 5: for i in range(10000,100000): sum = 0 order = function(i,N) for j in order: sum = sum + pow(j,N) if sum == i: print(i)
第4章-18 猴子选大王
#猴子选大王 num = eval(input()) ls = [] for i in range(1,num + 1): ls.append(i) p = 0 index = 0 while len(ls) > 1: p = 0 while p < len(ls): if (index + 1) % 3 == 0: ls.pop(p) p = p - 1 index = 0 else: index = index + 1 p = p + 1 for t in ls: print(t)
第4章-19 矩阵运算
num = eval(input()) count = 1 ls = [] sum = 0 while count <= num: strs = input().split(" ") ls.append(strs) count = count + 1 for i in range(len(ls)): for j in range(len(ls[i])): if not(i + j == num - 1 or i == num - 1 or j == num - 1): sum = sum + int(ls[i][j]) print(sum)
第4章-20 求矩阵各行元素之和
m,n = map(int,input().split(" ")) count = 1 ls = [] while count <= m: rows= input().split(" ") ls.append(rows) count = count + 1 for i in range(len(ls)): sum = 0 for j in range(len(ls[i])): sum = sum + int(ls[i][j]) print(sum)
第4章-21 判断上三角矩阵
T = eval(input()) count = 1 num = 0 total = [] dimensions = [] while count <= T: dimension = eval(input()) dimensions.append(dimension) matrix = [] i = 1 while i <= dimension: rows = list(map(int,input().split(" "))) matrix.append(rows) i = i + 1 total.append(matrix) count = count + 1 for j in range(len(total)): flag = False cnt = 0 for p in range(dimensions[j]): for q in range(dimensions[j]): if p > q and total[j][p][q] != 0: flag = False cnt = cnt + 1 break else: if p < q: continue elif p == q and total[j][p][q] != 0: flag = True continue elif p == q and total[j][p][q] == 0: flag = False cnt = cnt + 1 continue elif p > q and total[j][p][q] == 0: flag = True continue if flag and cnt == 0: print("YES") else: print("NO")
第4章-22 找鞍点
#找鞍点(最大行值&最小列值) import numpy as np dimension = eval(input()) count = 1 matrix = [] cnt = 0 while count <= dimension: rows = list(map(int,input().split(" "))) matrix.append(rows) count = count + 1 for row in range(len(matrix)): row_max = max(matrix[row]) for col in range(len(matrix[row])): if matrix[row][col] == row_max: col_min = min(np.array(matrix)[:,col])#将二维列表转为numpy array类型,取出特定一列 if matrix[row][col] == col_min: print(row,col) cnt = cnt + 1 else: break else: continue if not cnt: print("NONE")
第4章-23 求矩阵的局部极大值
#矩阵局部极大值 M,N = map(int,input().split(" ")) count = 1 matrix = [] cnt = 0 while count <= M: rows = list(map(int,input().split(" "))) matrix.append(rows) count = count + 1 for row in range(len(matrix)): for col in range(len(matrix[row])): if row == 0 or row == len(matrix) - 1 or col == 0 or col == len(matrix[row]) - 1: continue else: if matrix[row][col] > matrix[row - 1][col] and matrix[row][col] > matrix[row + 1][col]\ and matrix[row][col] > matrix[row][col - 1] and matrix[row][col] > matrix[row][col + 1]: print(matrix[row][col],row + 1,col + 1) cnt = cnt + 1 if not cnt: print("None",len(matrix),len(matrix[0]))
第4章-24 打印九九口诀表
#九九口诀表 N = eval(input()) for i in range(1,N + 1): for j in range(1,i + 1): print(i,"*",j,"=","{:^4}".format(i * j),sep = '',end = '') print("\n")
第4章-25 输出三角形字符阵列
num = eval(input()) i = num start = ord('A') while i > 0: count = 0 for j in range(i): print(chr(start + j),end = ' ') count = count + 1 if count == i: print("\n") start = start + i i = i - 1
第4章-26 求1!+3!+5!+……+n!
factor = 1 sum = 0 num = eval(input()) for i in range(1,num + 1,2): res = 1 for j in range(1,i + 1): res = res * j sum = sum + res print("n=",num,",s=",sum,sep = '')
第4章-27 二维数组中每行最大值和每行和
nums = list(map(int,input().split(" "))) matrix = [] index = 0 for i in range(3): j = 0 rows = [] while j < 3: rows.append(nums[index]) index = index + 1 j = j + 1 matrix.append(rows) for p in range(len(matrix)): sum = 0 max = matrix[p][0] for q in range(len(matrix[p])): print("{:4}".format(matrix[p][q]),end = '') sum = sum + matrix[p][q] if matrix[p][q] > max: max = matrix[p][q] print("{:4}".format(max),"{:4}".format(sum))
第4章-28 矩阵转置
nums = list(map(int,input().split(" "))) matrix = [] index = 0 count = 0 for i in range(3): j = 0 rows = [] while j < 3: rows.append(nums[index]) index = index + 1 j = j + 1 matrix.append(rows) for m in range(len(matrix)): for n in range(m + 1,len(matrix[m])): t = matrix[m][n] matrix[m][n] = matrix[n][m] matrix[n][m] = t for p in range(len(matrix)): for q in range(len(matrix[p])): print("{:4}".format(matrix[p][q]),end = '') count = count + 1 if count % 3 == 0: print("\n")
第4章-29 找出不是两个数组共有的元素
#不是两个数组共有的元素 i = 1 ls = [] not_common = [] while i <= 2: strs = list(map(int,input().split(" "))) ls.append(strs[1::]) i = i + 1 if len(ls[0]) >= len(ls[1]): for j in ls[1]: if not (j in mot_common) and j not in ls[0]: not_common.append(j) else: continue for k in ls[0]: if not(k in not_common) and k not in ls[1]: not_common.append(k) else: for j in ls[0]: if not(j in not_common) and j not in ls[1]: not_common.append(j) else: continue for k in ls[1]: if not(k in not_common) and k not in ls[0]: not_common.append(k) for key in not_common: print(key,end = ' ')
第4章-30 找完数
#找完数 m,n = map(int,input().split(" ")) all = [] factors = [] count = 0 for i in range(m,n + 1): factor = [] sum = 0 part = [] for j in range(1,i): if i % j == 0: factor.append(j) factors.append(factor) for t in factor: sum = sum + t part.append(i) part.append(sum) all.append(part) for p in range(len(all)): if all[p][0] == all[p][1]: count = count + 1 print(all[p][0],"= ",end = '') for r in range(len(factors[p])): if r == 0: print(factors[p][r],end = '') else: print(" +",factors[p][r],end = '') else: continue print("\n",end = '') if not count: print("None")