《Python程序设计与算法基础教程》——江红 余青松
第三章_上机实践
import math
j = 0
for m in range(101, 201):
k = int(math.sqrt(m))
for i in range(2, k + 2):
if m % i == 0:
break
if i == k + 1:
print(m, end = ' ' )
j += 1
if j % 10 == 0:
print()
n = int(input("请输入图形的行数:"))
for i in range(0, n):
for j in range(0, 10- i):
print(" ", end = " ")
for j in range(0, 2 * i + 1):
print("*", end = " ")
print("\n")
import math
print("三位数中所有的水仙花数为:")
for i in range(100, 1000):
n1 = i // 100
n2 = (i % 100)//10
n3 = i % 10
if (math.pow(n1, 3) + math.pow(n2, 3) + math.pow(n3, 3) == i):
print(i, end= " ")
print("1~1000之间所有的完数有,其因子为:")
for n in range(1, 1001):
total = 0
j = 0
factors = []
for i in range(1, n):
if (n % i == 0):
factors.append(i)
total += i
if (total == n):
print("{0}:{1}".format(n, factors))
m = int(input("请输入整数m:"))
n = int(input("请输入整数n:"))
while (m != n):
if (m > n):
m = m - n
else:
n = n - m
print(m)
total = 0
for i in range(1, 101):
total += i
print("1~100之和为:", total)
j = 0
for i in range(2000, 3000):
if((i % 4 == 0 and i % 100 != 0) or i % 400 == 0):
j += 1
print(i, end = " ")
if (j % 18 == 0):
print()
n = int(input("请输入一个整数:"))
count = 0
Sn1 = 0
Sn2 = 0
Sn = 0
for i in range(1, n + 1):
count += 1
if (count == 1):
Sn1 += 2 * i -1
if (count == 2):
i = -i
count = 0
Sn2 += 2 * i + 1
Sn = Sn1 +Sn2
print(Sn)
n = int(input("请输入一个整数:"))
Sn = 0
for i in range(1, n + 1):
An = 1 / i
Sn += An
print(Sn)
print("***九九乘法表***")
count = 0
for i in range(1, 10):
for j in range(1, 10):
print("{}*{}={}".format(i, j, i * j), end=' ')
count += 1
if count % 9 == 0:
print()
print("***下三角九九乘法表***")
for i in range(1, 10):
for j in range(1, 10):
print("{}*{}={} ".format(i, j, i * j), end=' ')
if i == j:
break
print("")
print("***上三角九九乘法表***")
for i in range(1, 10):
for j in range(1, 10):
if j < i:
print(end=" ")
continue
print("{}*{}={} ".format(i, j, i * j), end=' ')
print("")
print("矩阵九九乘法表:")
for i in range(1, 10):
s = ''
for j in range(1, 10):
s += "{0:1}*{1:1}={2:2} ".format(i, j, i * j)
print(s)
print("下三角九九乘法表")
for i in range(1, 10):
s = ''
for j in range(1, 10):
s += str.format("{0:1}*{1:1}={2:2} ", i, j, i*j)
if j == i:
break
print(s)
print("上三角九九乘法表:")
for i in range(1, 10):
s = ''
for j in range(1, 10):
if j < i:
print(end=" ")
continue
s += str.format("{0:1}*{1:1}={2:2} ", i, j, i*j)
print(s)
import math
A = int(input("请输入三角形的边A: "))
B = int(input("请输入三角形的边B: "))
C = int(input("请输入三角形的边C: "))
if (A + B > C and A + C > B and B + C > A):
perimeter = A + B + C
h = 1 / 2 * perimeter
area = math.sqrt(h * (h - A) * (h - B) * (h - C))
print("三角形三边分别为: a = {}, b = {}, c = {}".format(A, B, C))
print("三角形的周长 = {}, 面积 = {}".format(perimeter, area))
else:
print("无法构成三角形!")
import math
x = float(input("请输入x: "))
if x >= 0:
y = (x ** 2 - 3 * x)/(x + 1) + 2 * math.pi + math.sin(x)
else:
y = math.log((-5 * x), math.e) + 6 * math.sqrt(math.fabs(x) + math.e ** 4) - (x + 1) ** 3
print("方法三: x ="+ str(x) + " y = " + str(y))
import math
while (1):
a = float(input("请输入系数a: "))
b = float(input("请输入系数b: "))
c = float(input("请输入系数c: "))
if (a == 0 and b == 0):
print("此方程无解!")
elif (a == 0 and b != 0):
x = -c / b
print("此方程有一个实根: ", x)
elif (b ** 2 - 4 * a * c == 0):
x1 = x2 = -b / (2 * a)
print("此方程有两个相同实根: {}和{}".format(x1, x2))
elif (b ** 2 - 4 * a * c > 0):
x1 = - b / (2 * a) + (math.sqrt(b ** 2 - 4 * a * c) / (2 * a))
x2 = - b / (2 * a) - (math.sqrt(b ** 2 - 4 * a * c) / (2 * a))
print("此方程有两个不等实根:{}和{}".format(x1, x2))
else:
print("此方程有两个共轭复根:{0}+{1}i和{0}-{1}i".format(- b / (2 * a), (math.sqrt(4 * a * c - b ** 2) / (2 * a))))
import math
j = 1
whileCount = 1
n = int(input("请输入非负整数n:"))
while (n < 0):
n = int(input("请输入非负整数n:"))
x = n
if (n == 0):
print("0! = 1")
else:
for i in range(1, n + 1):
j *= i
print(" for循环:{}! = {}".format(n, j))
while (n):
whileCount *= n
n -= 1
print("while循环:{}! = {}".format(x, whileCount))
s = i = 1
while (i <= x):
s *= i
i += 1
print("while循环:{}! = {}".format(x, s))
import random
a = random.randint(0, 100)
b = random.randint(0, 100)
print("整数1 = {}, 整数2 = {}".format(a, b))
if a > b:
m = a
n = b
else:
m = b
n = a
r = m % n
while r != 0:
m = n
n = r
r = m % n
m = n
leastCommonMultiple = int(a * b / m)
print("最大公约数 = {}, 最小公倍数 = {}".format(m, leastCommonMultiple))