当前位置:   article > 正文

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

python程序设计与算法基础教程微课版第四章答案

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

2.

def generate(L) : #生成杨辉三角的 一行
    List = [1]
    for x in range(1,len(L)) :
        List.append(L[x] +L[x-1])
    List.append(1)
    return List

def printList(List, W):
    s = " "
    for x in List:
        s += str(x) + " "
    print(s.center(W))
    
List = [1]
numRow = int(input("输入行数: "))
width = numRow * 4 #打印宽度
for x in range(numRow) :
    printList(List, width)
    List = generate(List)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3.

import math
A = float(input("请输入直角三角形的直角边A(>0): "))
B = float(input("请输入直角三角形的直角边B(>0): "))
C = math.sqrt(A*A + B*B)
print(str.format("直角三角形三边分别为: a={0:.1f}, b={1:.1f}, c={2:.1f}", A, B, C))
perimeter = A+B+C
area = 1/2 * A * B
print(str.format("三角形的周长 = {0:.1f},  面积 = {1:.1f}", perimeter, area))
sinA = A / C
sinB = B / C
Aangle = round(math.asin(sinA) * 180 / math.pi, 0)
Bangle = round(math.asin(sinB) * 180 / math.pi, 0)
print(str.format("三角形两个锐角的度数分别为: {0:.1f} 和 {1:.1f}", Aangle, Bangle))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4.

import random
a = random.randint(0,100)
b = random.randint(0,100)
c = random.randint(0,100)
print(str.format("a = {0}, b= {1}, c={2}", a, b, c))
temp = 0
if (a > b) :
    temp = a
    a = b
    b = temp
if (a > c) :
     temp = a
     a = c
     c = temp
if (b > c) :
    temp = b
    b = c
    c = temp
print(str.format("(方法一)升序值: a = {0}, b= {1}, c={2}", a, b, c))

#方法二
a = min(a, b, c)
b = sum([a, b, c]) - max(a, b, c) - min(a, b, c)
c = max(a, b, c)
print(str.format("(方法二)升序值: a = {0}, b= {1}, c={2}", a, b, c))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

5.

salary = int(input("请输入有固定工资收入的党员的月工资:"))
if(salary <= 3000) :
    f = 0.005 * salary
elif(salary  > 3000 and salary <= 5000) :
    f = 0.01 * salary
elif(salary > 5000 and salary <= 10000) :
    f = 0.015 * salary
else :
    f = 0.02 * salary
print(str.format("月工资 = {0}, 交纳党费 = {1:.1f}", salary, f))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6.

x = float(input("请输入操作数x: "))
y = float(input("请输入操作数y: "))
operator = input("请输入操作符:")
if operator == '+':
        result = x + y
if operator == '-' :
        result = x - y
if operator == '*' :
        result = x * y
if operator == '/' :
    if y == 0:
         print("分母=0,零除异常!")
    else :
          result = x / y
if operator == '%' :
    if y == 0:
        print("分母=0,零除异常!")
    else :
        result = x % y
print(str.format("{0:.1f} {1} {2:.1f} = {3:.1f}", x, operator, y, result))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

7.

a = float(input("请输入三角形的边a :"))
b = float(input("请输入三角形的边b :"))
c = float(input("请输入三角形的边c :"))
if (a < 0 or b < 0 or c < 0 or a + b <= c or a + c <= b or b + c <= a):
    print("构不成三角形")
else :
    if(a == b == c) :
        print("该三角形为等边三角形")
    if((a == b and a != b and b != c) or (b == c and a != b and a != c) or (a == c and c != b and a != b)) :
        print("该三角形为等腰三角形")
    if(c*c == a*a + b*b or a*a == c*c + b*b or b*b == a*a + c*c) :
        print("该三角形为直角三角形")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

8.

h = int(input("请输入总头数:"))
f = int(input("请输入总脚数(必须是偶数):"))
while(f % 2 != 0) :
    f = int(input("请输入总脚数(必须是偶数):"))
def way1(h, f) :
    r = f / 2 - h
    c = h - r
    if(r < 0 or c < 0) :
        return '无解'
    return int(c), int(r)

def way2(h, f) :
    for c in range(0, h+1) :
        r = h - c
        if (2*c + 4*(r) == f) :
            return int(c), int(r)
    return '无解'

if(h > 0 and f > 0 and f % 2 == 0) :
    if way1(h, f) == '无解':
        print("无解")
    else :
        print(str.format("方法一:鸡:{0}只,兔{1}只", way1(h,f)[0], way1(h,f)[1])) #way1(h,f)[0]表示way1方法返回的第一个值
        print(str.format("方法二:鸡:{0}只,兔{1}只", way2(h,f)[0], way2(h,f)[1])) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

9.

import math
x = float(input("请输入x : "))
s = 1
y = x
i = 1
while y >= pow(10, -6) :
    y = pow(x, i) / math.factorial(i)
    s += y
    i += 1
print(str.format("Pow(e,x) = {0}", s))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

10.

a = int(input("请输入实数a : "))
if a<0 : print("输入错误")
else :
    x = a / 2
    y = (x + a/x) / 2
    while(abs(y-x) >= pow(10, -6)) :
        x = y
        y = (x + a/x) / 2
print(y)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

11.

for i in range(1,1001) :
    if(i % 3 == 2 and i % 5 == 3 and i % 7 == 2) :
        print(i, end = " ")
  • 1
  • 2
  • 3

12.

up = 0
down = 100
total = 0
for i in range(1, 11) :
    total += up + down
    down = up = down / 2
print(str.format("小球在第10次落地时,共经过{0}米,第10次反弹{1}米", total, up))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

13.

nums = 1
for i in range(8, 0, -1) :
    print(str.format("第{0}天桃子数:{1}", i, nums))
    nums = 2 * (nums + 1)
  • 1
  • 2
  • 3
  • 4

14.

import random
n = random.randint(1, 10)
S = 0
T = 1
for i in range(1, n+1) :
    S += T
    T = T*10+1
print(str.format("n = {0}, Sn = {1}", n, S))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

满怀希望就会所向披靡。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/402550
推荐阅读
相关标签
  

闽ICP备14008679号