noj第二季小段代码
后续准备更完100题
11.给定经纬度计算地球上两点之间的距离
记得转换为弧度制
a1=float(input(""))
b1=float(input(""))
a2=float(input(""))
b2=float(input(""))
pi=3.14159265359
a1=a1*pi/180
b1=b1*pi/180
a2=a2*pi/180
b2=b2*pi/180
a=a2-a1
b=b2-b1
import math
def haversine(a):
return math.sin(a/2)**2
a=haversine(a)
b=haversine(b)
c1=math.cos(a1)
c2=math.cos(a2)
d=a+c1*c2*b
d=math.acos(1-2*d)*6371
print("%.4fkm"%d)
12.指定精度舍入
f=float(input())
n=int(input())
print(f"{f:.{n}f}")
13.直角坐标转换为极坐标
x=float(input())
y=float(input())
import math
p=math.sqrt(x**2+y**2)
c=math.acos(x/p)
print("%.4f" %p,end=",")
print("%.4f" %c)
14.勾股定理
import math
a = int(input())
b = int(input())
if a == b:
if math.sqrt(a ** 2 + b ** 2) == int(math.sqrt(a ** 2 + b ** 2)):
print("c")
else:
print("non")
elif a > b:
if math.sqrt(a ** 2 + b ** 2) == int(math.sqrt(a ** 2 + b ** 2)):
print("c")
elif math.sqrt(a ** 2 - b ** 2) == int(math.sqrt(a ** 2 - b ** 2)):
if a ** 2 - b ** 2 >= b:
print("b")
else:
print("a")
else:
print("non")
else:
if math.sqrt(a ** 2 + b ** 2) == int(math.sqrt(a ** 2 + b ** 2)):
print("c")
elif math.sqrt(b ** 2 - a ** 2) == int(math.sqrt(b ** 2 - a ** 2)):
if b ** 2 - a ** 2 >= a ** 2:
print("b")
else:
print("a")
else:
print("non")
15.两个分数的加、减、乘、除
这题不考虑分母为零,分子分母为负数的情况
a1=int(input())
b1=int(input())
a2=int(input())
b2=int(input())
#我自己写的欧几里得算法
def gcd(a,b):
if b>a:
return gcd(b,a)
else :
c=a%b
if c==0:
return b
else :
return gcd(c,b)
def sum(a,b,c,d):
s1=a*d+c*b
s2=b*d
s=gcd(s1,s2)
s1=s1//s
s2=s2//s
return "%s/%s"%(s1,s2)
def subtraction(a,b,c,d):
s1 = a * d - c * b
s2 = b * d
s = gcd(s1, s2)
s1 = s1 // s
s2 = s2 // s
return "%s/%s" % (s1, s2)
def multiplication(a,b,c,d):
s1 = a * c
s2 = b * d
s = gcd(s1, s2)
s1 = s1 // s
s2 = s2 // s
return "%s/%s" % (s1, s2)
def division(a,b,c,d):
s1 = a * d
s2 = b * c
s = gcd(s1, s2)
s1 = s1 // s
s2 = s2 // s
return "%s/%s" % (s1, s2)
# c=gcd(a1,b1)
# d=gcd(a2,b2)
# a_1=a1//c
# b_1=b1//c
# a_2=a2//d
# b_2=b2//d
if a1*b1>0 and a2*b2>0:
sum1=sum(a1,b1,a2,b2)
print("(%d/%d)+(%d/%d)=%s"%(a1,b1,a2,b2,sum1))
# 若按下方这句输出,“=”后会有空格
# print("(%d/%d)+(%d/%d)="%(a1,b1,a2,b2),sum(a1,b1,a2,b2))
subtract1=subtraction(a1,b1,a2,b2)
print("(%d/%d)-(%d/%d)=%s"%(a1,b1,a2,b2,subtract1))
multiplication1=multiplication(a1,b1,a2,b2)
print("(%d/%d)*(%d/%d)=%s"%(a1,b1,a2,b2,multiplication1))
division1=division(a1,b1,a2,b2)
print("(%d/%d)/(%d/%d)=%s"%(a1,b1,a2,b2,division1))
elif a1*b1<0 and a2*b2<0:
pass
else :
pass
16.圆柱
h=float(input())
pi=3.14159265359
r=float(input())
area=pi*r*r
print("{:.4f}".format(area*h))
print("{:.4f}".format(area*2+2*pi*r*h))
17.计算狗的年龄
n = float(input())
if n<=2:
print(10.5*n)
else:
n=(n-2)*4+21
print(int(n))
18.比率
python的库内有gcd函数可以用,不过你也可以写一写
import math
f=float(input())
F=int(f)
def gcd(a,b):
if b>a:
return gcd(b,a)
else :
c=a%b
if c==0:
return b
else :
return gcd(c,b)
if f-F==0:
print(F)
else:
# sf=str(f-F)
# sf=sf[2:]
L=len(str(F))
sf=str(f)[L+1:]
l=len(sf)
sf=int(sf)
sf_10=10**l
F=F*sf_10+sf
g=gcd(F,sf_10)
sf_10=sf_10//g
F=F//g
print("{}/{}".format(F,sf_10))
19.RGB转换为HSV
这题绝对是信息没给全,R,G,B要先除以255化到【0,1】区间内
r=float(input())/255
g=float(input())/255
b=float(input())/255
v=max(r,g,b)
m=min(r,g,b)
s=0
h=0
if v==0:
s=0
else:
s=(v-m)/v
if v==r:
h=60*(0+(g-b)/(v-m))
elif v==g:
h=60*(2+(b-r)/(v-m))
else:
h=60*(4+(r-g)/(v-m))
if h<0:
h=h+360
print("{:.4f},{:.4f}%,{:.4f}%".format(h,100*s,100*v))
20.风寒指数
v=float(input())
t=float(input())
v=v**0.16
print(round(13.12+0.6215*t-11.37*v+0.3965*t*v))