第2章-1 计算 11+12+13+...+m
sum = 0 start = 11 end = eval(input()) for i in range(start,end+1): sum = sum + i print("sum =",sum)
第2章-2 计算分段函数[1]
num = eval(input()) if num == 0: num = format(num,'.1f') print("f(",num,") = ",num,sep = '') else: res = 1.0/num num = format(num,'.1f') print("f(",num,") = ",format(res,'.1f'),sep = '')
第2章-3 阶梯电价
power_consumption = eval(input()) if power_consumption < 0: print("Invalid Value!") else: if power_consumption <= 50: cost = 0.53*power_consumption else: cost = 0.53*50+(power_consumption-50)*(0.53+0.05) print("cost = ",format(cost,'.2f'),sep = '')
第2章-4 特殊a串数列求和
a,n = map(int,input().split(" ")) sum = 0 for i in range(n): res = a*(pow(10,i)*(n-i)) sum = sum + res print("s = ",sum,sep = '')
第2章-5 求奇数分之一序列前N项和
num = eval(input()) sum = 0 count = 1 for i in range(num): sum = sum + 1/count count = count + 2 print("sum = ",format(sum,'.6f'),sep = '')
第2章-6 求交错序列前N项和
num = eval(input()) sum = 0 count = 1 for i in range(num): if count % 2 == 0: sum = sum - count/(2*count - 1) else: sum = sum + count/(2*count - 1) count = count + 1 print(format(sum,'.3f'))
第2章-7 产生每位数字相同的n位数
A,B = map(int,input().split(",")) A = str(A) A = A*B print(int(A))
第2章-8 转换函数使用
a,b = map(int,input().split(",")) if b == 2: res = int(str(a),2) elif b == 8: res = int(str(a),8) elif b == 16: res = int(str(a),16) print(res)
第2章-9 比较大小
a,b,c = map(int,input().split(" ")) list = [a,b,c] list.sort() print(list[0],"->",list[1],"->",list[2],sep = "")
第2章-10 输出华氏-摄氏温度转换表
lower,higher = map(int,input().split(" ")) if lower <= higher and lower <= 100 and higher <= 100: print("fahr celsius") i = lower while i <= higher: c = 5*(i - 32)/9 print(i,format(c,'>6.1f')) i = i + 2 else: print("Invalid.")
第2章-11 求平方与倒数序列的部分和
m,n = map(int,input().split(" ")) sum = 0 start = m end = n while start <= end: sum = sum + 1/start + pow(start,2) start = start + 1 print("sum = ",format(sum,".6f"))
第2章-12 输出三角形面积和周长
a,b,c = map(int,input().split(" ")) s = (a + b + c)/2 if a + b > c and a + c > b and b + c > a: area = pow(s * (s - a) * (s - b) * (s - c),1/2) perimeter = a + b + c print("area = ",format(area,".2f"),";perimeter = ",format(perimeter,".2f"),sep = "") else: print("These sides do not correspond to a valid triangle")
第2章-13 分段计算居民水费
water_consumption = eval(input()) if water_consumption <= 15: cost = (4*water_consumption)/3 print(format(cost,".2f")) else: cost = 2.5 * water_consumption - 17.5 print(format(cost,".2f"))
第2章-14 求整数段和
a,b = map(int,input().split(" ")) sum = 0 count = 0 for i in range(a,b + 1): print(format(i,">5"),end = "") count = count + 1 sum = sum + i if count % 5 == 0: print("\n") if count % 5 == 0: print("Sum = ",sum) else: print("\n") print("Sum = ",sum)