I originally planned to start some basic algorithms directly, but later found that it is still relatively difficult, so let's start from the basic skills.
1. The area of a circle
print(“A={:.4f}”.format(s))
This line of code is to output the variable s according to the requirements of the format string, where {:.4f} indicates that the output floating-point number retains 4 decimal places. The specific meaning is as follows:
{}: curly braces are used to mark the start and end of the output field;
:: The colon signifies the start of a format specifier;
.4f: Indicates a floating-point number format specifier, where:
.: Decimal point, used to specify the precision;
4: Indicates that 4 decimal places are reserved;
f: Indicates the floating-point number type.
r=float(input())
s=3.14159*r*r
print("A={:.4f}".format(s))
2. The distance between two points
Writing method one:
from math import sqrt
x1,y1=map(float,input().split())
x2,y2=map(float,input().split())
s=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
print("{:.4f}".format(s))
Writing method two:
x1,y1=map(float,input().split())
x2,y2=map(float,input().split())
s=(x2-x1)**2+(y2-y1)**2
print("{:.4f}".format(s**0.5))
3. Banknotes and coins
Writing method one:
When python calculates decimals, decimal overflow may occur, so the previous data must be enlarged and then reduced.
t1 = [ 100,50,20,10,5,2]
t2 = [1,0.50,0.25,0.10,0.05,0.01]
n = float(input())*100#放大100倍
print('NOTAS:')
for i in t1:
i = i*100
if n>=i:
print('{:.0f} nota(s) de R$ {:.2f}'.format(n//i,i/100))
n = n-(n//i)*i
else:
print('0 nota(s) de R$ {:.2f}'.format(i/100))
if n != 0:
print('MOEDAS:')
for i in t2:
i = i * 100
if n>=i:
print('{:.0f} moeda(s) de R$ {:.2f}'.format(n//i,i/100))
n = n-(n//i)*i
else:
print('0 moeda(s) de R$ {:.2f}'.format(i/100))
# print(n)
Writing method two:
t1 = [ 100,50,20,10,5,2]
t2 = [1,0.50,0.25,0.10,0.05,0.01]
n = float(input())*100
print('NOTAS:')
for i in t1:
i = i*100
if n>=i:
print('{:.0f} nota(s) de R$ {:.2f}'.format(n//i,i/100))
n = n-(n//i)*i
else:
print('0 nota(s) de R$ {:.2f}'.format(i/100))
if n != 0:
print('MOEDAS:')
for i in t2:
i = i * 100
if n>=i:
print('{:.0f} moeda(s) de R$ {:.2f}'.format(n//i,i/100))
n = n-(n//i)*i
else:
print('0 moeda(s) de R$ {:.2f}'.format(i/100))
# print(n)
Four, multiple
Implement the swap function in python
Two methods:
1.
def swap(a, b):
return b, a
2、
a,b=b,a
code show as below:
a,b=map(int,input().split())
if a<b:
a,b=b,a
if a%b :
print("Nao sao Multiplos")
else:
print("Sao Multiplos")
5. Quadratic equation formula in one variable
import math ---->math.sqrt
import math
a,b,c=map(float,input().split())
if b**2-4*a*c<0 or a==0:
print("Impossivel calcular")
else:
r1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
r2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
print("R1 = {:.5f}".format(r1))
print("R2 = {:.5f}".format(r2))
Six, simple sorting
Sort sorted, read into array
a=list(map(int,input().split()))
b=sorted(a)
for i in b: print(i)
print()
for i in a: print(i)
7. Tax
Writing method one:
exit() exits the program
a=float(input())
ans=0
if a<2000:
print("Isento")
exit()
elif a<3000:
ans=(a-2000.00)*0.08
elif a<4500:
ans=80+(a-3000.00)*0.18
else:
ans=350+(a-4500)*0.28
print("R$ {:.2f}".format(ans))
Writing method two:
for data in datas[::-1]:
This line of code is a for loop statement, which is used to traverse the datas list, where [::-1] is a slicing syntax, indicating that the list is reversed.
Specifically, datas[::-1] will generate a new list with the same elements as the original list datas, but in reverse order. For example, for the list [1, 2, 3, 4, 5], [::1] produces the list [5, 4, 3, 2, 1].
Therefore, for data in datas[::-1]: will traverse from the last element of the datas list to the first element. In each loop, the variable data will sequentially fetch an element in the datas list, which can be processed according to the actual situation.
round(res,2)
round(res,2) is a function in Python for retaining a specified number of digits after the decimal point of a floating-point number, where res is the floating-point number to retain, and 2 is the number of decimal places to retain. For example, round(3.14159, 2) results in 3.14.
datas = [[0,2000,0],[2000.01,3000,0.08],[3000.01,4500,0.18],[4500.01,4500.01,0.28]]
pay = float(input())
res = 0
for data in datas[::-1]:
if pay >= data[0]:
t = pay-data[0]+0.01
# print("t",t)
res += data[-1] * t
pay -= t
if res == 0:
print("Isento")
else:
print("R$ {:.2f}".format(round(res,2)))
8. Game time 2
It can also record input like this! ! !
n = input().split()
h1 = int(n[0])
m1 = int(n[1])
h2 = int(n[2])
m2 = int(n[3])
if m2 < m1 :
m2 += 60
h2 -= 1
if h2 < h1:
h1 -=24
if h1 == h2 and m1 == m2:
h2 += 24
print("O JOGO DUROU", h2-h1, "HORA(S) E", m2-m1, "MINUTO(S)")