写在前面:
有编程基础的同学可以直接刷(不会语法再看文档,若没有那就边看书边刷
浙大版《Python 程序设计》题目集4~7章
题目链接:浙大版《Python 程序设计》题目集
文章目录
- 第4章-1 生成3的乘方表 (15 分)
- 第4章-2 统计素数并求和 (20 分)
- 第4章-3 猴子吃桃问题 (15 分)
- 第4章-4 验证“哥德巴赫猜想” (20 分)
- 第4章-5 求e的近似值 (15 分)
- 第4章-6 输出前 n 个Fibonacci数 (15 分)
- 第4章-7 统计学生平均成绩与及格人数 (15 分)
- 第4章-8 求分数序列前N项和 (15 分)
- 第4章-9 查询水果价格 (15 分)
- 第4章-10 最大公约数和最小公倍数 (15 分)
- 第4章-11 判断素数 (20 分)
- 第4章-12 求满足条件的斐波那契数 (30 分)
- 第4章-13 求误差小于输入值的e的近似值 (20 分)
- 第4章-14 统计字符 (15 分)
- 第4章-15 换硬币 (20 分)
- 第4章-16 jmu-python-判断是否构成三角形 (10 分)
- 第4章-17 水仙花数(20 分) (20 分)
- 第4章-18 猴子选大王 (20 分)
- 第4章-19 矩阵运算 (20 分)
- 第4章-20 求矩阵各行元素之和 (15 分)
- 第4章-21 判断上三角矩阵 (15 分)
- 第4章-22 找鞍点 (20 分)
- 第4章-23 求矩阵的局部极大值 (15 分)
- 第4章-24 打印九九口诀表 (15 分)
- 第4章-25 输出三角形字符阵列 (15 分)
- 第4章-26 求1!+3!+5!+……+n! (10 分)
- 第4章-27 二维数组中每行最大值和每行和 (10 分)
- 第4章-28 矩阵转置 (10 分)
- 第4章-29 找出不是两个数组共有的元素 (20 分)
- 第4章-30 找完数 (20 分)
- 第5章-1 输出星期名缩写 (70 分)
- 第5章-2 图的字典表示 (20 分)
- 第5章-3 四则运算(用字典实现) (30 分)
- 第5章-4 分析活动投票情况 (20 分)
- 第5章-5 统计字符出现次数 (20 分)
- 第5章-6 统计工龄 (20 分)
- 第5章-7 列表去重 (40 分)
- 第5章-8 能被3,5和7整除的数的个数(用集合实现) (30 分)
- 第5章-9 求矩阵鞍点的个数 (30 分)
- 第5章-10 两数之和 (30 分)
- 第5章-11 字典合并 (40 分)
- 第6章-1 输入列表,求列表元素和(eval输入应用) (10 分)
- 第6章-2 一帮一 (15 分)
- 第6章-3 列表或元组的数字元素求和 (20 分)
- 第6章-4 列表数字元素加权和(1) (40 分)
- 第6章-5 列表元素个数的加权和(1) (40 分)
- 第6章-6 求指定层的元素个数 (40 分)
- 第6章-7 找出总分最高的学生 (15 分)
- 第6章-8 输出全排列 (20 分)
- 第7章-1 词频统计 (30 分)
第4章-1 生成3的乘方表 (15 分)
import math
def main():
n = int(input())
for i in range(n + 1):
print("pow(3,%d) = %d"%(i,math.pow(3,i)))
if __name__ == '__main__':
main()
第4章-2 统计素数并求和 (20 分)
def isprime(n):
if n <= 1:
return False
for i in range(2,int(n**0.5)+1,1):
if n % i == 0:
return False
return True
def main():
a,b = map(int,input().split(' '))
cnt = 0
s = 0
for i in range(a,b+1,1):
if(isprime(i)):
cnt+=1
s += i
print("%d %d"%(cnt,s))
if __name__ == '__main__':
main()
第4章-3 猴子吃桃问题 (15 分)
def main():
n = int(input())
s = 1
for i in range(n-1):
s = (s + 1)*2
print(s)
if __name__ == '__main__':
main()
第4章-4 验证“哥德巴赫猜想” (20 分)
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n % i == 0:
return False
return True
def main():
n = int(input())
for i in range(2,n//2+1):
if(isprime(i) and isprime(n - i)):
print("%d = %d + %d"%(n,i,n-i))
break
if __name__ == '__main__':
main()
第4章-5 求e的近似值 (15 分)
def jecal(n):
s = 1
for i in range(1,n):
s = s * i
return s
def cal(n):
s = 0
for i in range(1,n + 2):
s += 1/jecal(i)
return s
def main():
n = int(input())
res = cal(n)
print("%.8f"%res)
if __name__ == '__main__':
main()
第4章-6 输出前 n 个Fibonacci数 (15 分)
def show(n):
list1 = [1,1]
for i in range(2,n):
list1.append(list1[i - 1] + list1[i - 2])
cnt = 0
for i in range(n):
cnt += 1
print("%11d"%list1[i],end="")
if cnt % 5 == 0:
print("")
if cnt % 5 != 0:
print("")
def main():
n = int(input())
if n < 1 :
print("Invalid.")
else:
show(n)
if __name__ == '__main__':
main()
第4章-7 统计学生平均成绩与及格人数 (15 分)
def main():
n = int(input())
avg = 0
cnt = 0
if n == 0:
print("average = %.1f\ncount = %d" % (avg, int(cnt)))
exit(0)
list1 = list(map(int,input().split()))
for i in range(n):
avg += list1[i]
if list1[i] >= 60:
cnt += 1
avg /= n
print("average = %.1f\ncount = %d"%(avg,int(cnt)))
if __name__ == '__main__':
main()
第4章-8 求分数序列前N项和 (15 分)
def cal(n):
fz = 2
fm = 1
ans = 0
for i in range(1,n+1):
ans += fz/fm
t = fz
fz += fm
fm = t
return ans
def main():
n = int(input())
res = cal(n)
print("%.2f"%res)
if __name__ == '__main__':
main()
第4章-9 查询水果价格 (15 分)
prices = [3,2.5,4.1,10.2]
def main():
print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
list1 = list(map(int,input().split()))
for i in range(len(list1)):
if i == 5:
exit(0)
x = list1[i]
if x == 0:
exit(0)
elif x >= 1 and x <= 4:
print("price = %.2f"%prices[x-1])
else:
print("price = 0.00")
exit(0)
if __name__ == '__main__':
main()
第4章-10 最大公约数和最小公倍数 (15 分)
def gcd(a,b):
if b == 0:
return a
return gcd(b,a%b)
def main():
a,b = map(int,input().split())
d = gcd(a,b)
s = a*b/gcd(a,b)
print("%d %d"%(d,s))
if __name__ == '__main__':
main()
第4章-11 判断素数 (20 分)
def isprime(n):
if n <= 1:
return False
for i in range(2,int(n**0.5)+1):
if n % i == 0:
return False
return True
def main():
n = int(input())
for i in range(n):
x = int(input())
if(isprime(x)):
print("Yes")
else:
print("No")
if __name__ == '__main__':
main()
第4章-12 求满足条件的斐波那契数 (30 分)
def main():
n = int(input())
a = 1
b = 1
while n > a:
t = a + b
a = b
b = t
print(a)
if __name__ == '__main__':
main()
第4章-13 求误差小于输入值的e的近似值 (20 分)
def jecal(n):
s = 1
for i in range(1,n+1):
s *= i
return s
def cal(error):
i = 1
s = 1
while 1/jecal(i) >= error:
s += 1/jecal(i)
i += 1
return s+1/jecal(i)
def main():
error = float(input())
print("%.6f"%cal(error))
if __name__ == '__main__':
main()
第4章-14 统计字符 (15 分)
def main():
s = input()
let,bl,di,ot = 0,0,0,0
while 1:
for i in s:
if i.isalpha():
let += 1
elif i == ' ' or i == '\n':
bl += 1
elif i.isdigit():
di += 1
else:
ot += 1
if let + bl + di + ot >= 10:
break
s = input()
bl += 1
print("letter = %d, blank = %d, digit = %d, other = %d"%(let,bl,di,ot))
if __name__ == '__main__':
main()
第4章-15 换硬币 (20 分)
def main():
n = int(input())
count = 0
for a in range(n,0,-1):
for b in range(n,0,-1):
for c in range(n,0,-1):
if a*5 + b*2 + c == n:
print("fen5:%d, fen2:%d, fen1:%d, total:%d" %(a, b, c, a + b + c))
count += 1
print("count = %d"%count)
if __name__ == '__main__':
main()
第4章-16 jmu-python-判断是否构成三角形 (10 分)
def main():
list1 = list(map(int,input().split()))
list1.sort()
if list1[0] + list1[1] > list1[2]:
print("yes")
else:
print("no")
if __name__ == '__main__':
main()
第4章-17 水仙花数(20 分) (20 分)
import math
def judge(n,cnt):
res = 0
y = n
while n!=0:
res += int(math.pow(n%10,cnt))
n//=10
return res == y
def main():
n = int(input())
for i in range(int(math.pow(10,n-1)),int(math.pow(10,n))):
if judge(i,n):
print(i)
if __name__ == '__main__':
main()
第4章-18 猴子选大王 (20 分)
def main():
n = int(input())
list1 = []
for i in range(1,n+1):
list1.append(i)
cnt = 1
i = 0
while 1:
if cnt == 3:
list1.remove(list1[i])
cnt = 1
continue
if len(list1) == 1:
break
cnt += 1
i = (i + 1)%len(list1)
print(list1[0])
if __name__ == '__main__':
main()
第4章-19 矩阵运算 (20 分)
def main():
n = int(input())
list1 = [[]]
for i in range(n):
list1.append([])
list1[i] = list(map(int,input().split()))
res = 0
for i in range(n):
for j in range(n):
if i + j + 1 == n or i == n - 1 or j == n - 1:
continue
res += list1[i][j]
print(res)
if __name__ == '__main__':
main()
第4章-20 求矩阵各行元素之和 (15 分)
def main():
n,m = map(int,input().split())
list1 = []
for i in range(n):
list1.append([])
list1[i] = list(map(int,input().split()))
for i in range(n):
res = 0
for j in range(m):
res += list1[i][j]
print(res)
if __name__ == '__main__':
main()
第4章-21 判断上三角矩阵 (15 分)
def solve():
n = int(input())
list1 = []
for i in range(n):
list1.append([])
list1[i] = list(map(int,input().split()))
flag = True
for i in range(n):
for j in range(i):
if list1[i][j] != 0:
flag = False
if flag:
print("YES")
else:
print("NO")
def main():
t = int(input())
while t:
t -= 1
solve()
if __name__ == '__main__':
main()
第4章-22 找鞍点 (20 分)
list1 = []
n = 0
def judge1(i,j):
maxi = list1[i][j]
for k in range(n):
if maxi < list1[i][k] and k != j:
return False
return True
def judge2(i,j):
minj = list1[i][j]
for k in range(n):
if minj > list1[k][j] and k != i:
return False
return True
def main():
global n
n = int(input())
for i in range(n):
list1.append([])
list1[i] = list(map(int,input().split()))
if n == 1:
print("0 0")
exit(0)
flag = True
for i in range(n):
for j in range(n):
if judge1(i,j) and judge2(i,j):
flag = False
print("%d %d"%(i,j))
break
if flag:
print("NONE")
if __name__ == '__main__':
main()
第4章-23 求矩阵的局部极大值 (15 分)
list1 = []
dx = [-1,0,1,0]
dy = [0,1,0,-1]
n,m = 0,0
def judge(x,y):
for i in range(4):
a = x + dx[i]
b = y + dy[i]
if a == x and b == y:
continue
if list1[x][y] <= list1[a][b]:
return False
return True
def main():
global n,m
n,m = map(int,input().split())
for i in range(n):
list1.append([])
list1[i] = list(map(int,input().split()))
flag = True
for i in range(n):
for j in range(m):
if i and j and i != n - 1 and j != m - 1 and judge(i,j):
flag = False
print("%d %d %d"%(list1[i][j],i+1,j+1))
if flag:
print("None %d %d"%(n,m))
if __name__ == '__main__':
main()
第4章-24 打印九九口诀表 (15 分)
def main():
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print("%d*%d=%-4d"%(j,i,i*j),end="")
print("")
if __name__ == '__main__':
main()
第4章-25 输出三角形字符阵列 (15 分)
def main():
n = int(input())
c = 'A'
for i in range(n,0,-1):
for j in range(i):
print("%s "%c,end="")
c = chr(ord(c) + 1)
print("")
if __name__ == '__main__':
main()
第4章-26 求1!+3!+5!+……+n! (10 分)
def jepow(n):
s = 1
for i in range(1,n+1):
s *= i
return s
def main():
n = int(input())
s = 0
for i in range(1,n+2,2):
s += jepow(i)
print("n=%d,s=%d"%(n,s))
if __name__ == '__main__':
main()
第4章-27 二维数组中每行最大值和每行和 (10 分)
def main():
list1 = list(map(int,input().split()))
list2 = []
n = 9
for i in range(3):
list2.append([])
for j in range(3):
list2[i].append(list1[i*3+j])
for i in range(3):
s = 0
for j in range(3):
s += list2[i][j]
print("%4d"%list2[i][j],end="")
print("%4d%4d"%(max(list2[i]),s))
if __name__ == '__main__':
main()
第4章-28 矩阵转置 (10 分)
def main():
list1 = list(map(int,input().split()))
list2 = []
n = 9
for i in range(3):
list2.append([])
for j in range(3):
list2[i].append(list1[i*3+j])
for i in range(3):
for j in range(3):
print("%4d"%list2[j][i],end="")
print("")
if __name__ == '__main__':
main()
第4章-29 找出不是两个数组共有的元素 (20 分)
def main():
list1 = list(map(int,input().split()))
list2 = list(map(int,input().split()))
list1.remove(list1[0])
list2.remove(list2[0])
set1 = set([])
flag = False
for i in range(len(list1)):
if list1[i] not in list2 and list1[i] not in set1:
if flag:
print(" ",end="")
set1.add(list1[i])
print("%d"%list1[i],end="")
flag = True
for i in range(len(list2)):
if list2[i] not in list1 and list2[i] not in set1:
if flag:
print(" ",end="")
set1.add(list2[i])
print("%d"%list2[i],end="")
flag = True
if __name__ == '__main__':
main()
第4章-30 找完数 (20 分)
def show(list):
for i in range(len(list)):
if i:
print(" + ",end="")
print("%d"%list[i],end="")
def work(n):
s = 0
list1 = []
for i in range(1,int(n**0.5)+1):
if n % i == 0:
s += i+n//i
list1.append(i)
list1.append(n/i)
list1.sort()
list1.remove(n)
if s == 2*n:
print("%d = "%n,end="")
show(list1)
print("")
return True
return False
def main():
n,m = map(int,input().split())
flag = True
for i in range(n,m+1,1):
if work(i):
flag = False
if flag:
print("None")
if __name__ == '__main__':
main()
第5章-1 输出星期名缩写 (70 分)
strs = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
def main():
n = int(input())
print(strs[n-1])
if __name__ == '__main__':
main()
第5章-2 图的字典表示 (20 分)
def main():
n = int(input())
s = 0
edges = 0
for i in range(n):
list1 = eval(input())
for j in list1:
list2 = list1[j]
for k in list2:
s += list2[k]
edges += 1
print("%d %d %d"%(n,edges,s))
if __name__ == '__main__':
main()
第5章-3 四则运算(用字典实现) (30 分)
res = {'+':'x+y','-':'x-y','*':'x*y','/':"x/y if y!= 0 else 'divided by zero'"}
x = int(input())
op = input()
y = int(input())
ans = eval(res[op])
if type(ans) == str:
print(ans)
else:
print("%.2f"%ans)
第5章-4 分析活动投票情况 (20 分)
def main():
set1 = set(list(map(int,input().split(','))))
flag = False
for i in range(6,11):
if i not in set1:
if flag:
print(" ",end="")
print("%d"%i,end="")
flag = True
if __name__ == '__main__':
main()
第5章-5 统计字符出现次数 (20 分)
def main():
s = input()
c = input()
print(s.count(c))
if __name__ == '__main__':
main()
第5章-6 统计工龄 (20 分)
def main():
n = int(input())
list1 = list(map(int,input().split()))
dict1 = {}
for i in range(n):
dict1[list1[i]] = dict1.get(list1[i],0)+1
list_keys = sorted(dict1.keys())
for key in list_keys:
print("%d:%d"%(key,dict1[key]))
if __name__ == '__main__':
main()
第5章-7 列表去重 (40 分)
def main():
list1 = eval(input())
set1 = set([])
flag = False
for i in range(len(list1)):
if list1[i] not in set1:
if flag:
print(" ",end="")
print("%d"%list1[i],end="")
set1.add(list1[i])
flag = True
if __name__ == '__main__':
main()
第5章-8 能被3,5和7整除的数的个数(用集合实现) (30 分)
def judge(n):
if n % 3 == 0 and n % 5 == 0 and n % 7 == 0:
return True
return False
def main():
a,b = map(int,input().split())
cnt = 0
for i in range(a,b+1):
if judge(i):
cnt += 1
print(cnt)
if __name__ == '__main__':
main()
第5章-9 求矩阵鞍点的个数 (30 分)
n = 0
list1 = []
list2 = []
def judge1(x,y):
maxi = list1[x][y]
return maxi == max(list1[x])
def judge2(x,y):
minj = list1[x][y]
return minj == min(list2[y])
def main():
global n
n = int(input())
for i in range(n):
list1.append([])
list1[i] = list(map(int,input().split()))
for i in range(n):
list2.append([])
for j in range(n):
list2[i].append(list1[j][i])
cnt = 0
for i in range(n):
for j in range(n):
if judge1(i,j) and judge2(i,j):
cnt += 1
print(cnt)
if __name__ == '__main__':
main()
第5章-10 两数之和 (30 分)
def main():
list1 = list(map(int,input().split(',')))
tar = int(input())
dict1 = {}
flag = True
for i in range(len(list1)):
a = list1[i]
if dict1.get(tar - a,-1) != -1:
b = dict1[tar-a]
a = i
if a > b:
a,b=b,a
print("%d %d"%(a,b))
flag = False
break
else:
dict1[a] = i
if flag:
print("no answer")
if __name__ == '__main__':
main()
第5章-11 字典合并 (40 分)
def main():
dict1 = eval(input())
dict2 = eval(input())
for key,value in dict2.items():
if key in dict1.keys():
dict1[key] += value
else:
dict1[key] = value
list1,list2 = [],[]
for key in dict1.keys():
if type(key) == int:
list1.append(key)
else:
list2.append(key)
list1.sort()
list2.sort()
print("{",end="")
flag = False
for i in range(len(list1)):
if flag:
print(",",end="")
print("%d:%d"%(list1[i],dict1[list1[i]]),end="")
flag = True
for i in range(len(list2)):
if flag:
print(",",end="")
print("\"%s\":%d"%(list2[i],dict1[list2[i]]),end="")
flag = True
print("}",end="")
if __name__ == '__main__':
main()
第6章-1 输入列表,求列表元素和(eval输入应用) (10 分)
def main():
list1 = eval(input())
res = 0
for i in range(len(list1)):
res += list1[i]
print(res)
if __name__ == '__main__':
main()
第6章-2 一帮一 (15 分)
def main():
n = int(input())
list1 = []
list2 = []
for i in range(n):
list3 = input().split()
a = int(list3[0])
b = list3[1]
list1.append(a)
list2.append(b)
for i in range(n//2):
for j in range(n-1,n//2-1,-1):
if list1[i] + list1[j] == 1:
print("%s %s"%(list2[i],list2[j]))
list1[j] = 2
break
if __name__ == '__main__':
main()
第6章-3 列表或元组的数字元素求和 (20 分)
def dfs(list1):
s = 0
if type(list1) == int:
s += list1
elif type(list1) == list or type(list1) == tuple:
for x in list1:
s += dfs(x)
return s
def main():
list1 = eval(input())
res = 0
res = dfs(list1)
print(res)
if __name__ == '__main__':
main()
第6章-4 列表数字元素加权和(1) (40 分)
def dfs(list1,cnt):
s = 0
if type(list1) == int:
s += cnt*list1
else:
for x in list1:
s += dfs(x,cnt+1)
return s
def main():
list1 = eval(input())
res = dfs(list1,0)
print(res)
if __name__ == '__main__':
main()
第6章-5 列表元素个数的加权和(1) (40 分)
def dfs(list1,cnt):
s = 0
if type(list1) == int:
s += cnt
else:
for x in list1:
s += dfs(x,cnt+1)
return s
def main():
list1 = eval(input())
res = dfs(list1,0)
print(res)
if __name__ == '__main__':
main()
第6章-6 求指定层的元素个数 (40 分)
n = 0
def dfs(list1,cnt):
s = 0
if type(list1) == int and cnt == n:
s += 1
elif type(list1) == list or type(list1) == tuple:
for x in list1:
s += dfs(x,cnt+1)
return s
def main():
global n
list1 = eval(input())
n = int(input())
res = dfs(list1,0)
print(res)
if __name__ == '__main__':
main()
第6章-7 找出总分最高的学生 (15 分)
def main():
n = int(input())
ids,names,g = [],[],[]
for i in range(n):
list1 = input().split()
ids.append(list1[0])
names.append(list1[1])
g.append(int(list1[2])+int(list1[3])+int(list1[4]))
maxg = g.index(max(g))
print("%s %s %d"%(names[maxg],ids[maxg],g[maxg]))
if __name__ == '__main__':
main()
第6章-8 输出全排列 (20 分)
n = 0
list1 = []
st = [False]*10
def dfs(u):
if u == n+1:
for x in list1:
print(x,end="")
print("")
for i in range(1,n+1):
if st[i] == 0:
st[i] = True
list1.append(i)
dfs(u+1)
list1.pop()
st[i] = False
def main():
global n
n = int(input())
dfs(1)
if __name__ == '__main__':
main()
第7章-1 词频统计 (30 分)
import sys
s = sys.stdin.read()
s = s[:s.find("#")]
for k in set([i for i in s if i.isalnum()==False and i!='_']):
s = s.replace(k,' ')#除字母数字和_外都看作空格
t = s.rstrip().lower().split()#去除末尾空格#.split()生成单词列表
d = {}
for i in t:
d[i[:15]] = d.get(i[:15],0)+1
res = sorted(d.items(),key=lambda x:(-x[1],x[0]))#value降序key升序#转为相反数即升序
print(len(res))
for i in range(int(len(res)/10)):
print(str(res[i][1])+":"+res[i][0])