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==0and 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:
ssh命令指定端口print(str.format("{0:2} * {1:1} = {2:>2}", a, b, a*b), end =' ')
print()
10.
c程序设计语言免费阅读import math
a =float(input("请输⼊三⾓形的边a :"))
b =float(input("请输⼊三⾓形的边b :"))
c =float(input("请输⼊三⾓形的边c :"))
if(a >0and b >0and c >0and 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))
用phpstudy搭建网站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:"))
java编程思想 脚本之家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))
⽅法三(条件运算语句):
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.
大学python入门教程
import math
a =float(input("请输⼊系数a : "))
b =float(input("请输⼊系数b : "))
c =float(input("请输⼊系数c : "))
delt = b**2-4*a*c
if(a ==0and b ==0):
print("此⽅程⽆解!")
elif(a ==0and 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))pycharm2017 永久激活码
⽅法⼆(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.
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))满怀希望就会所向披靡。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论