Python第三次作业代码

问题 A: 【顺序程序】甲流疫情死亡率

a=int(input())
b=int(input())
print(round(b/a,3),"%",sep='')

问题 B: 【顺序结构】计算多项式的值

x=float(input())
a=float(input())
b=float(input())
c=float(input())
d=float(input())
y= a*x*x*x+b*x*x+c*x+d;
print(round(y,7))

问题 C: 【顺序程序】数字倒序

a = int(input())
x=(a%10)*1000+((a//10)%10)*100+((a//100)%10)*10+a//1000
print(x)

问题 D: 【顺序程序】时间换算

a = int(input())
h=a//3600
m = (a % 3600) // 60
s = a % 60
print(h, m,s)

问题 E: 【顺序程序】买铅笔

# 从键盘输入10元纸币的张数
n = int(input())

# 铅笔的价格
pencil = 0.45

# 计算最多能买的铅笔数
max_pencil = n * 10 // pencil

# 计算剩下的钱
left_money = n * 10 % pencil

# 输出结果
print("buy={:.0f} left={:.1f}".format(max_pencil, left_money))
#format通过字符串中的花括号{}来识别和替换字符串,由此达到格式化字符串的目的。填充内容位置的识别,有按顺序自动替换、通过数字指定顺序、通过关键字指定位置三种方式。

问题 F: 【顺序程序】神奇的数字

char = input()
cnt = int(input())

# 将字符转换为相应的ASCLL码值
char_1 = ord(char)

# 将数值转换为相应的字符
cnt_char = chr(cnt)

# 计算数值之和
sum_1 = char_1 + cnt

# 输出结果
print(char_1, cnt_char, sum_1)

问题 G: 【顺序程序】求三角形面积

import math

# 输入三角形的三边长
a = float(input())
b = float(input())
c = float(input())

# 计算半周长
p = (a + b + c) / 2

# 使用海伦公式计算面积
s = math.sqrt(p * (p - a) * (p - b) * (p - c))

# 输出结果,保留两位小数
print('%.2f' % s)

问题 H: 【顺序程序】计算线段长度

Xa = float(input())
Ya = float(input())
Xb = float(input())
Yb = float(input())
l = ((Xb - Xa) ** 2 + (Yb - Ya) ** 2) ** 0.5
print('%.3f' % l)

问题 I: 【顺序程序】计算三角形面积

# 输入三角形三个顶点的坐标
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())
x3 = float(input())
y3 = float(input())

# 计算三角形的边长
a = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
b = ((x3 - x2) ** 2 + (y3 - y2) ** 2) ** 0.5
c = ((x1 - x3) ** 2 + (y1 - y3) ** 2) ** 0.5

# 计算半周长
p = (a + b + c) / 2

# 使用海伦公式计算面积
S = (p * (p - a) * (p - b) * (p - c)) ** 0.5

# 输出结果,精确到小数点后两位
print('%.2f' % S)

题目来源:http://py.lboj.com.cn/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值