AcWing 0x00. 语法基础课【Python3】版题解-顺序/判断/循环语句

AcWing语法基础课【Python3】版题解-顺序/判断/循环语句

【AcWing】

【AcWing 语法基础课】

【AcWing 0x00. 语法基础课【Python3】版题解-顺序/判断/循环语句】

【AcWing 0x01. 语法基础课【Python3】版题解-内置数据结构/字符串】

【AcWing 0x02. 语法基础课【Python3】版题解-函数/类和对象/常用库】

顺序语句

例题

【AcWing 1. A+B】

s = input().split()
print(int(s[0])+int(s[1]))

解析:Python3里input()函数输入的字符串,所以要先切割成列表再转换成整型数求和。

另一种写法是:

A, B = map(int, input().split())
print(A+B)

解析:

  1. 在分割字符串后直接把列表中的字符映射为整数,返回迭代器。
  2. 序列分解:只要变量的总数和结构与序列吻合,就可以通过赋值操作把任何序列(或可迭代的对象)分解为单独的变量。

【AcWing 608. 差】

最简单的方法:

A = int(input())
B = int(input())
C = int(input())
D = int(input())
DIFERENCA = A*B - C*D
print("DIFERENCA = "+str(DIFERENCA))

解析:使用强制类型转换str()函数把整型数转换成了字符串。

更简单的写法是:

A = int(input())
B = int(input())
C = int(input())
D = int(input())
print(f"DIFERENCA = {
     A*B - C*D}")

解析:在Python3.6及其以上,可使用f字符串在字符串中使用变量,具体方法是在引号前加上字母f,再将要插入的变量放在花括号内,这样Python3显示字符串时,会把每个变量都替换为其值。

也可使用格式化字符串的方法:

A = int(input())
B = int(input())
C = int(input())
D = int(input())
print(f"DIFERENCA = %d"%(A*B - C*D))

解析:格式化字符串的基本用法将一个值插入到一个有字符串格式符%的字符串中。

符号 描述
%c 格式化字符及其ASCII码
%s 格式化字符串
%d 格式化整数
%f 格式化浮点数字,可指定小数点后的精度

若用列表和循环,也可:

x = []
for i in range(4):
    x.append(int(input()))
diferenca = x[0]*x[1]-x[2]*x[3]
print("DIFERENCA = "+str(diferenca))

【AcWing 604. 圆的面积】

pi = 3.14159
R = float(input())
A = pi*R**2
print('A=%.4f'%A)

解析:Python3中'%.nf'%A是格式化浮点数字,可以把浮点数A转换成保留n位小数的字符串。

f字符串的方法:

import math
R = float(input())
A = round(math.pi, 5)*R**2
print(f'A={
     round(A, 4)}')

解析:导入math标准库,使用math.π常量,round(A, n)返回浮点数A保留n位小数(四舍五入)的值。

f字符串直接保留n位小数的方法:

import math
R = float(input())
print(f'A={
     round(math.pi, 5)*R**2:.4f}')

解析:f"{A:.nf}"是把浮点数A保留n位小数的方法。

【AcWing 606. 平均数1】

A = float(input())
B = float(input())
MEDIA = A*3.5/11 + B*7.5/11
# MEDIA = (A*3.5 + B*7.5)/11
print("MEDIA = %.5f"%MEDIA)

解析: 加权平均值即将各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数。

【AcWing 609. 工资】

number = input()
hours = int(input())
salary = float(input())

salary = hours * salary
print("NUMBER = "+number)
print("SALARY = U$ %.2f"%salary)

解析:直接模拟,输出两个字符串拼接可以直接使用+

【AcWing 615. 油耗】

km = int(input())
l = float(input())
M = km/l
print('%.3f km/l'%M)

解析:直接模拟。

【AcWing 616. 两点间的距离】

s1 = input().split()
s2 = input().split()
distance = ((float(s2[0])-float(s1[0]))**2 +(float(s2[1])-float(s1[1]))**2)**0.5
print('%.4f'%distance)

解析:Python3里对x开根号用的是x**2

第二种方法是使用math标准库里的sqrt()函数:

import math
s1 = input().split()
s2 = input().split()
distance = math.sqrt((float(s2[0])-float(s1[0]))**2 +(float(s2[1])-float(s1[1]))**2)
print('%.4f'%distance)

解析:math.sqrt(x)执行对x开平方的操作。

【AcWing 653. 钞票】

第一种方法是使用保存所有面值的钞票的数量然后再输出:

N = int(input())
money = {
   100:0, 50:0, 20:0, 10:0, 5:0, 2:0, 1:0}
print(N)
for key in money:
    money[key] = N // key
    N = N % key
    print('%d nota(s) de R$ %d,00'%(money[key], key))

解析:使用字典来保存所有面值的钞票的数量,key是钞票面值,value是该面值的钞票的数量。

第二种方法是不保存所有面值的钞票的数量,直接计算并输出:

N = int(input())
money = [100, 50, 20, 10, 5, 2, 1]
print(N)
for i in money:
    print('%d nota(s) de R$ %d,00'%(N//i, i))
    N = N % i

解析:格式化字符串时,将一个元组的值插入到一个有字符串格式符%的字符串中。

【AcWing 654. 时间转换】

N = int(input())
hours = N // 3600
minutes = (N - hours*3600) // 60
seconds = N - hours*3600 - minutes*60
print(str(hours)+':'+str(minutes)+':'+str(seconds))

解析:直接模拟。

更简单的方法是:

N = int(input())
print("%d:%d:%d"%(N//60//60, N//60%60, N%60))
习题

【AcWing 605. 简单乘积】

x = int(input())
y = int(input())
print(f"PROD = {
     x * y}")

解析:直接模拟。

【AcWing 611. 简单计算】

s1 = input().split()
s2 = input().split()
print("VALOR A PAGAR: R$ %.2f"%(float(s1[1])*float(s1[2])+float(s2[1])*float(s2[2])))

解析:直接模拟。

【AcWing 612. 球的体积】

R = float(input())
print("VOLUME = %.3f"%((4/3)*3.14159*R**3))

解析:直接模拟。

【AcWing 613. 面积】

A, B, C = map(float, input().split())
print("TRIANGULO: %.3f"%(0.5*A*C))
print("CIRCULO: %.3f"%(3.14159*C*C))
print("TRAPEZIO: %.3f"%((A+B)*C*0.5))
print("QUADRADO: %.3f"%(B*B))
print("RETANGULO: %.3f"%(A*B))

解析:直接模拟。

【AcWing 607

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值