《Python程序设计与算法基础教程》第三章上机实践

《Python程序设计与算法基础教程》——江红 余青松

第三章_上机实践

# ch3 P59_3
# *实现101-201输出所有的素数*
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()

# ch3 P60_4
# *输出三角形*
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")

# ch3 P60_5
# * 水仙花数是一个三位数,三位数各位的立方之和等于三位数本身 *
import math # 调用模块,访问math模块中*
print("三位数中所有的水仙花数为:")
for i in range(100, 1000):
    n1 = i // 100  # //地板除法,求小于a与b的商的最大整数
    n2 = (i % 100)//10
    n3 = i % 10
    if (math.pow(n1, 3) + math.pow(n2, 3) + math.pow(n3, 3) == i): # pow(x,y)返回x的y次幂的值
        print(i, end= " ")

# ch3 P60_6
# * 如果一个数恰好等于它的真因子之和,则称该数为“完全数”。各个小于它的约数(真约数,列出某数的约数,去掉该数本身,剩下的就是它的真约数)的和等于它本身的自然数叫做完全数(Perfect number),又称完美数或完备数。*
# 找出1~1000之间所有的完数
print("1~1000之间所有的完数有,其因子为:")
for n in range(1, 1001):
    total = 0
    j = 0
    factors = []
    for i in range(1, n):
        if (n % i == 0): # 找出n的所有约数并且出去它本身
            factors.append(i) # 存入数组中
            total += i
    if (total == n): # 完全数的要求是所有的真约数之和为n本身
        print("{0}:{1}".format(n, factors))

# ch3 P60_7
# * 求任意两个整数的最大公约数 *
m = int(input("请输入整数m:"))
n = int(input("请输入整数n:"))
while (m != n):
    if (m > n):
        m = m - n
    else:
        n = n - m
print(m)
# 上机实践
# ch3 P60_1
# * 计算1~100之和 *
total = 0
for i in range(1, 101):
    total += i
print("1~100之和为:", total)

# 上机实践
# ch3 P61_5
# * 输出2000~3000之间的所有闰年 *
# * 判断闰年的条件是:年份能被4整除但不能被100整除,或者能被400整除*
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()

# ch3 P61_6
# * 计算Sn = 1-3+5-7+9—11+。。。 *
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)

# 上机实践
# ch3 P61_7
# * 计算Sn=1+1/2+1/3+... *
n = int(input("请输入一个整数:"))
Sn = 0
for i in range(1, n + 1):
    An = 1 / i
    Sn += An
print(Sn)

# 上机实践
# ch3 P61_8
# * 九九乘法表 *
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("")

# 上机实践
# ch3 P61_8
# * 九九乘法表 *
print("矩阵九九乘法表:")
for i in range(1, 10):
    s = ''
    for j in range(1, 10):
        # {2:<2} # {}叫做占位符
        s += "{0:1}*{1:1}={2:2} ".format(i, j, i * j)
        # s += str.format("{0:1}*{1:1}={2:<2} ", i, j, i*j) # 把后面的格式变成字符串,然后字符串的连接赋给s
    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)

# 上机实践
# ch3 P61_9
# * 输入三角形三条边,判断是否可以构成三角形 *
# * 条件:1.每条边长均大于0,并且任意两边之和大于第三边
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("无法构成三角形!")

# 上机实践
# ch3 P61_10
# *  输入x,根据如下公式,计算分段函数y的值 *
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))

# 上机实践
# ch3 P61_11
# *  输入x,根据如下公式,计算分段函数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))))

# 上机实践
# ch3 P61_12
# *  输入x,根据如下公式,计算分段函数y的值 *
import math
j = 1
whileCount = 1
n = int(input("请输入非负整数n:"))
while (n < 0):
    n = int(input("请输入非负整数n:"))
x = n  # 再用x指代输入的n,防止在后面的操作中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))

# 上机实践
# ch3 P61_13
# *  编写程序,产生两个0~100之间(包含0和1000)的随机整数a和b *
# * 求这两个整数的最大公约数和最小公倍数 *
# * 方法:辗转相除法求最大公约数 *
import random
a = random.randint(0, 100)
b = random.randint(0, 100)
print("整数1 = {}, 整数2 = {}".format(a, b))
if a > b: # 1.对于已知的两个正整数m、n,使得m>n。
    m = a
    n = b
else:
    m = b
    n = a
r = m % n # 2.m除以n得余数r
while r != 0:
    m = n
    n = r
    r = m % n # 若r != 0,则令m = n,n = r,继续相除得到新的余数r。
    # 若仍然r != 0,则重复此过程,直到r = 0 为止。最后的m就是最大公约数。
m = n
leastCommonMultiple = int(a * b / m) # 最小公倍数就是已知的两个正整数之积除以最大公约数的商
print("最大公约数 = {}, 最小公倍数 = {}".format(m, leastCommonMultiple))

你可能感兴趣的:(python)