AcWing语法基础课【Python3】版题解-顺序/判断/循环语句
【AcWing 0x00. 语法基础课【Python3】版题解-顺序/判断/循环语句】
【AcWing 0x01. 语法基础课【Python3】版题解-内置数据结构/字符串】
【AcWing 0x02. 语法基础课【Python3】版题解-函数/类和对象/常用库】
顺序语句
例题
s = input().split()
print(int(s[0])+int(s[1]))
解析:Python3里input()
函数输入的字符串,所以要先切割成列表再转换成整型数求和。
另一种写法是:
A, B = map(int, input().split())
print(A+B)
解析:
- 在分割字符串后直接把列表中的字符映射为整数,返回迭代器。
- 序列分解:只要变量的总数和结构与序列吻合,就可以通过赋值操作把任何序列(或可迭代的对象)分解为单独的变量。
最简单的方法:
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))
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位小数的方法。
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)
解析: 加权平均值即将各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数。
number = input()
hours = int(input())
salary = float(input())
salary = hours * salary
print("NUMBER = "+number)
print("SALARY = U$ %.2f"%salary)
解析:直接模拟,输出两个字符串拼接可以直接使用+
。
km = int(input())
l = float(input())
M = km/l
print('%.3f km/l'%M)
解析:直接模拟。
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开平方的操作。
第一种方法是使用保存所有面值的钞票的数量然后再输出:
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
解析:格式化字符串时,将一个元组的值插入到一个有字符串格式符%的字符串中。
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))
习题
x = int(input())
y = int(input())
print(f"PROD = {
x * y}")
解析:直接模拟。
s1 = input().split()
s2 = input().split()
print("VALOR A PAGAR: R$ %.2f"%(float(s1[1])*float(s1[2])+float(s2[1])*float(s2[2])))
解析:直接模拟。
R = float(input())
print("VOLUME = %.3f"%((4/3)*3.14159*R**3))
解析:直接模拟。
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))
解析:直接模拟。