Python程序设计与算法基础教程(第二版)微课版第三章上机实践答案

Python程序设计与算法基础教程(第二版)微课版第三章上机实践答案

2.

Sum = 0
for i in range(1, 101) :
    Sum += i
print("1+2+3+...+100 = ", Sum)

3.

Sum = 0
for i in range(10, 0, -1) :
    Sum += i
print("10+9+8+...+1 = ", Sum)

4.

Sum = 0
for i in range(1, 100, 2) :
    Sum += i
print("1+3+5+7+...+99 = ", Sum)

5.

Sum = 0
for i in range(2,101, 2) :
    Sum += i
print("2+4+6+8+...+100 = ", Sum)

6.

n = 0
for i in range(2000, 3001) :
    if ((i % 4 == 0 and i % 100 != 0) or (i % 400 == 0)) :
        print(i, end = ' ')
        n += 1
        if (n %10 == 0) : print()

7.

i = 1
S = 0
for n in range(1,100) :
    if(n % 2 != 0) :
        if(i % 2 == 0) :
            n = -(i * 2-1)
        else :
            n = ( i * 2-1)
        i += 1
        S += n
print("1-3+5-7+9-11+... = ", S)

8.

S = 0
for i in range(1, 101) :
    n = 1.0 / i
    S += n
print(str.format("1+1/2+1/3+... = {0: .2f}", S))

9.

打印上三角九九乘法表:

for a in range(1, 10) :
    for b in range(1, a+1) :
        print(str.format("{0:2} * {1:2} = {2:>2}", a, b, a*b), end = ' ')
    print()

打印矩形块九九乘法表:

for a in range(1, 10) :
    for b in range(1, 10) :
        print(str.format("{0:>2} * {1:1} = {2:>2}", a, b, a*b), end = ' ')
    print()

打印下三角九九乘法表:

for a in range(1, 10) :
    for b in range(1, 10) :
        if (b < a) : 
            print(end = '                  ')
        else :
            print(str.format("{0:2} * {1:1} = {2:>2}", a, b, a*b), end = ' ')
    print()

10.

import math
a = float(input("请输入三角形的边a :"))
b = float(input("请输入三角形的边b :"))
c = float(input("请输入三角形的边c :"))
if (a > 0 and b > 0 and c > 0 and a + b > c and a + c > b and b + c > a) :
    h = 1/2 * (a+b+c)
    print(str.format("三角形的三条边分别为:a = {0} b = {1} c = {2}", a, b, c))
    print(str.format("三角形的周长 = {0} 面积 = {1}", a+b+c, math.sqrt(h * (h-a) * (h-b) * (h-c))))
else :
    print("无法构成三角形!")

11.

方法一(单分支语句):

import math
x = float(input("请输入x:"))
print("方法一(单分支语句):")
if (x >= 0) :
    y = (x**2 - 3*x) / (x + 1) + 2*math.pi + math.sin(x)
if (x < 0) :
    y = math.log(-5*x) + 6 * math.sqrt(abs(x) + pow(math.e, 4)) - pow ((x + 1), 3)
print(str.format("x = {0},  y = {1}", 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) + 6 * math.sqrt(abs(x) + pow(math.e, 4)) - pow ((x + 1), 3)
print(str.format("x = {0},  y = {1}", x, y))

方法三(条件运算语句):

import math
x = float(input("请输入x:"))
y = (x**2 - 3*x) / (x + 1) + 2*math.pi + math.sin(x) if(x >= 0) \
else math.log(-5*x) + 6 * math.sqrt(abs(x) + pow(math.e, 4)) - pow ((x + 1), 3)
print(str.format("x = {0},  y = {1}", x, y))

12.

import math
a = float(input("请输入系数a : "))
b = float(input("请输入系数b : "))
c = float(input("请输入系数c : "))
delt = b**2 - 4*a*c
if (a == 0 and b == 0) :
    print("此方程无解!")
elif (a == 0 and b != 0) :
    print(str.format("此方程的解为:{0: .1f}", -c / b))
elif (b**2 - 4*a*c == 0) :
    print(str.format("此方程有两个相等虚根:{0:.1f}", -b / (2*a)))
elif (delt > 0) :
    print(str.format("此方程有两个不等虚根: {0:.1f} 和 {1:.1f}", (-b + math.sqrt(delt)) / (2*a), (-b - math.sqrt(delt)) / (2*a)))
else :
    #注意根号里不是b²-4ac,所以根号里加负号
   print("此方程有两个共轭复根: {0} 和 {1}" .format(complex( -b / (2*a), math.sqrt(-delt) / (2*a)), complex( -b / (2*a), -math.sqrt(-delt) / (2*a))))

13.

方法一(for循环):

n = int(input("请输入非负整数n:"))
while (n < 0) :
    n = int(input("请输入非负整数n:"))
S = 1
if (n == 0) : print("0! = 1")
if (n > 0) :
    for i in range(n, 0, -1) :
        S *= i
    print(str.format("{0}! = {1}", n, S))

方法二(while循环)

n = int(input("请输入非负整数n:"))
while (n < 0) :
    n = int(input("请输入非负整数n:"))
S = 1
if (n == 0) : print("0! = 1")
if (n > 0) :
    m = n
    while(m != 0) :
        S *= m
        m -=1
    print(str.format("{0}! = {1}", n, S))

14.

import random
a = random.randint(0,100)
b = random.randint(0,100)
S = a*b
print(str.format("整数1 = {0}, 整数2 = {1}", a, b))
if (a < b) :
    a, b = b, a
while (a%b != 0) :
    a,b = b,a%b
print("最大公约数 = {0}, 最小公倍数 = {1}".format(b, S/b))

满怀希望就会所向披靡。

你可能感兴趣的:(Python,python)