目录
7-1 生成3的乘方表
num = int(input())
for z in range(0,num+1):
print("pow(3,%d) = %d" % (z,pow(3,z)))
7-2 统计素数并求和
def SU(num):
if num < 2:
return False
if num < 4:
return True
for z in range(2,num):
if num % z ==0 :
return False
return True
M,N = map(int,input().split())
result = num = 0
for index in range(M,N+1):
if(SU(index)):
result += index
num += 1
print("%d %d" % (num,result))
7-3 猴子吃桃问题
N = int(input())
result = 1
for z in range(1,N):
result = 2 * result + 2
print(result)
7-4 求e的近似值
N = int(input())
result = flag = 1.0 # 不能用 1,得用1.0
for z in range(1,N+1):
flag /= z
result += flag
print("%.8f" % result)
7-5 输出前 n 个Fibonacci数
N = int(input())
if N < 1:
print("Invalid.")
else:
a = 0
b = 1
for z in range(1, N + 1):
print("%11d" % b, end='')
if z % 5 == 0:
print()
a, b = b, a+b
# 自定义函数 -> 递归法
# num1 第一个数,num2 第二个数, flag 为已经输出的次数
# def Fibonacci(num1, num2, flag):
# if flag > N:
# return
# print("%11d" % num2, end='')
# if flag % 5 == 0:
# print()
# Fibonacci(num2, num1 + num2, flag + 1)
# N = int(input())
# if N < 1:
# print("Invalid.")
# else:
# Fibonacci(0, 1, 1)
7-6 统计学生平均成绩与及格人数
num = int(input())
avg = passNum = 0
# num = 0 不能取,直接退出
if num > 0:
score = [int(x) for x in input().split()]
for index in score:
avg += index
if index >= 60:
passNum += 1
avg /= num
print("average = %.1f" % avg)
print("count = %d" % passNum)
7-7 求分数序列前N项和
N = int(input())
result = 0
num1 = 1
num2 = 2
for index in range(0,N):
result += num2 / num1
num1, num2 = num2, num1+num2
print("%.2f" % result)
# 分子,分母,当前循环次数, 递归法最后一个测试点会出现迭代过多
# def sumFigure(num1,num2,n):
# if n == N:
# return 0
# return num2/num1 + sumFigure(num2,num2+num1,n+1)
# print("%.2f" % sumFigure(1,2,0))
7-8 查询水果价格
fruit = {1:'apple', 2:'pear', 3:'orange', 4:'grape', 0:'exit'}
price = {'apple':3.00, 'pear':2.50, 'orange':4.10, 'grape':10.20}
for index in fruit:
print("[%d] %s" % (index, fruit[index]))
command = [int(x) for x in input().split()]
num = 0 # 统计查询次数
for index in command:
if index == 0 or num == 5:
break
money = 0.00
if 1 <= index <= 4:
money = price[fruit[index]]
print("price = %.2f" % money)
num += 1
7-9 最大公约数和最小公倍数
# 最大公约数
def GYS(num1, num2):
return num2 if num1 % num2 == 0 else GYS(num2, num1 % num2)
M, N = map(int, input().split())
key = GYS(max(M, N), min(M, N))
print("%d %d" % (key, M * N / key))
7-10 判断素数
from math import sqrt
def SU(number):
if number == 2:
return True
if number == 1 or number & 1 == 0:
return False
length = int(sqrt(number)) + 2
for z in range(3, length, 2):
if number % z == 0:
return False
return True
N = int(input())
for index in range(N):
num = int(input())
print("Yes") if SU(num) else print("No")
7-11 求满足条件的斐波那契数
N = int(input())
def Fibonacci(num1,num2):
if num2 > N:
return num2
return Fibonacci(num2,num1+num2)
print(Fibonacci(1,1))
7-12 求误差小于输入值的e的近似值
flag = float(input())
result = num1 = num2 = 1
while num1 > flag:
num1 /= num2
result += num1
num2 += 1
print("%.6f" % result)
7-13 换硬币
total = int(input())
count = 0
for fen5 in range(total//5,0,-1):
for fen2 in range(total//2,0,-1):
for fen1 in range(total,0,-1):
if fen5*5+fen2*2+fen1 == total:
count += 1
print("fen5:%d, fen2:%d, fen1:%d, total:%d" % (fen5,fen2,fen1,fen5+fen2+fen1))
print("count = %d" % count)
7-14 jmu-python-判断是否构成三角形
a,b,c = map(int,input().split())
if a+b<=c or a+c<=b or b+c<=a:
print("no")
else:
print("yes")
7-15 水仙花数(20 分)
N = int(input())
for z in range(pow(10,N-1),pow(10,N)):
result = 0
z1 = z
while z1 > 0:
result += pow(z1%10,N)
z1 //= 10
if result == z:
print(result)
7-16 求1!+3!+5!+……+n!
n = int(input())
s = 0
for z in range(1,n+1,2):
result = 1
for z1 in range(1,z+1):
result *= z1
s += result
print("n=%d,s=%d" % (z,s))
7-17 找出不是两个数组共有的元素
# 方便好写,实际程序运行没有俩个判断循环来的快
num1 = [int(x) for x in input().split()]
num1.pop(0)
num2 = [int(x) for x in input().split()]
num2.pop(0)
# 记录出现的元素
appear1 = {}
appear2 = {}
appear3 = {} # 记录所有的元素
for index in num1:
appear1[index] = 0
appear3[index] = 0
for index in num2:
appear2[index] = 0
appear3[index] = 0
output = 0 # 判断是否已经有输出
for index in appear3:
if index not in appear1 or index not in appear2:
if output == 1 :
print(' ', end='')
print(index, end='')
output = 1
7-18 找完数
import math
m, n = map(int,input().split())
flag = 0 # 判断有无完数
for num in range(m,n+1):
result = 1
keys = []
length = int(math.sqrt(num))+1 # 循环空间缩小,避免超时
for z in range(2,length):
if num % z ==0:
result += z
result += num//z
keys.append(z)
keys.append(num//z)
if result == num:
keys.sort()
print("%d = 1" % num, end='')
for z1 in keys:
print(" + %d" % z1, end='')
print()
flag = 1
if flag == 0:
print("None")
7-19 猴子选大王
# 约瑟夫环
n = int(input())
line = [n for n in range(1,n+1)]
die = 0
while len(line) > 1:
die += 3
while die > len(line):
die -= len(line)
line.pop(die-1)
die -= 1
print(line[0])
7-20 矩阵运算
n = int(input())
result = 0
for z in range(1,n):
nums = [int(x) for x in input().split()]
for index in range(0,n-1):
if index != n-z:
result += nums[index]
print(result)
7-21 求矩阵各行元素之和
m,n = map(int,input().split())
for z in range(0,m):
nums = [int(x) for x in input().split()]
print(sum(nums))
7-22 判断上三角矩阵
def flag(n,box):
for z in range(1,n):
for z1 in range(0,z):
if box[z][z1] != 0:
return 0
return 1
T = int(input())
for index in range(0,T):
boxs = []
N = int(input())
for index1 in range(0,N):
line = [int(x) for x in input().split()]
boxs.append(line)
if flag(N,boxs) == 1:
print("YES")
else:
print("NO")
7-23 找鞍点
n = int(input())
box = [] # 存储矩阵
# 输入 横坐标 and 纵坐标 判断是否符合要求
def lineMax(l,h):
for index in range(0,n):
if box[l][index] > box[l][h]:
return 0
return 1
def rankMin(l,h):
for index in range(0,n):
if box[index][h] < box[l][h]:
return 0
return 1
for z in range(0,n):
line = [int(x) for x in input().split()]
box.append(line)
output = 0
for z in range(0,n):
for z1 in range(0,n):
if lineMax(z1,z) == 1 and rankMin(z1,z) == 1:
print("%d %d" % (z1,z))
output = 1
if output == 0:
print("NONE")
7-24 打印九九口诀表
N = int(input())
for z in range(1,N+1):
for z1 in range(1,z+1):
print("%d*%d=%-4d" % (z1,z,z*z1), end='')
print()
7-25 求矩阵的局部极大值
M,N = map(int,input().split())
box = []
for z in range(0,M):
line = [int(x) for x in input().split()]
box.append(line)
flag = 0
for z in range(1,M-1):
for z1 in range(1,N-1):
if box[z][z1] > box[z][z1-1] and box[z][z1] >box[z][z1+1] and box[z][z1] > box[z+1][z1] and box[z][z1] > box[z-1][z1]:
flag += 1
print("%d %d %d" % (box[z][z1],z+1,z1+1)) # 下标从 1 开始
if flag == 0:
print("None %d %d" % (M,N))
7-26 输出三角形字符阵列
N = int(input())
flag = 65
for z in range(N,0,-1):
for z1 in range(0,z):
print("%c" % (chr(flag)), end=' ')
flag += 1
print()
7-27 二维数组中每行最大值和每行和
figure = [int(x) for x in input().split()]
for z in range(0,3):
numSum = maxFlag = figure[z*3]
print("%4d" % numSum, end='')
for z1 in range(1,3):
print("%4d" % figure[z*3+z1], end='')
numSum += figure[z*3+z1]
if figure[z*3+z1] > maxFlag:
maxFlag = figure[z*3+z1]
print("%4d%4d" % (maxFlag,numSum))
7-28 矩阵转置
figure = [int(x) for x in input().split()]
box = []
# N = 3, N为矩阵的长度,该题已经默认为3
for z in range(0,3):
for z1 in range(0,3):
print("%4d" % figure[z1*3+z], end='')
print()
7-29 找出总分最高的学生
N = int(input())
flag = []
scoreMax = 0
for z in range(0,N):
student = input().split()
score = int(student[2]) + int(student[3]) + int(student[4])
if score > scoreMax:
scoreMax = score
flag = student
print("%s %s %d" % (flag[1],flag[0],scoreMax))