西北工业大学NOJ Python程序设计作业11-20

各位同学,创作不易,能否在文末打赏一瓶饮料呢?(^ _ ^)(文章末尾右下角处)
在这里插入图片描述

西北工业大学NOJ-Python程序设计作业题解集合:
NOJ-Python程序设计:第1季:水题(Season 1-Easy) (1-10)
NOJ-Python程序设计:第2季:小段代码(Season 2-Snippet) (11-20)
NOJ-Python程序设计:第3季:循环(Season 3-Loop) (21-30)
NOJ-Python程序设计:第4季:枚举算法(Season 4-Enumeration algorithm) (31-40)
NOJ-Python程序设计:第5季:模块化(Season 5-Modularization) (41-50)
NOJ-Python程序设计:第6季:字符串(Season 6-String) (51-60)
NOJ-Python程序设计:第7季:列表与元组(Season 7-List and Tuple) (61-70)
NOJ-Python程序设计:第8季:集合与字典(Season 8-Sets and Dictionary) (71-80)
NOJ-Python程序设计:第9季:类(Season 9-Class) (81-90)
NOJ-Python程序设计:第10季:挑战算法(Season 10-Challenges) (91-100)

第2季:小段代码(Season 2-Snippet) (11-20)

img

前置知识点

建议大概了解下述函数库的基本运用之后再完成题目会更顺利。

math库
  • 导入函数库import math
  • 求余切:math.atan()的输入参数是弧度而不是度。
  • 求平方根:math.sqrt()
  • 求次方: math.pow(x,y)相当于求 x y x^y xy
  • 求反余弦值:math.acos()
fractions库

可以同时提供分子(numerator)和分母(denominator)给构造函数用于实例化Fraction类,但两者必须同时是int类型或者numbers.Rational类型,否则会抛出类型错误。当分母为0,初始化的时候会导致抛出异常ZeroDivisionError。(自带约分简化的功能)

  • 导入fractions库import fractions
  • 创建一个分数x=fractions.Fraction(2,3)
  • 提取分子、分母:a,c=x.numerator,x.denominator
  • 创建的分数可以直接+、-、*、÷
round函数:

round() 方法返回浮点数x的四舍五入值。(round函数并不总是四舍五入,会受计算机表示精度的影响,同时也受python版本影响)

语法:round( x [, n] )

  • x:数值表达式。
  • n:数值表达式,表示从小数点位数。(如果不填入参数n,则默认四舍五入到整数;如果填了n,表示四舍五入保留n位小数)

给定经纬度计算地球上两点之间的距离

img

import math
def hav(theta):
    return math.pow(math.sin(theta/2),2)
def Cal(w1,j1,w2,j2):
    w1=math.radians(w1)
    j1=math.radians(j1)
    w2=math.radians(w2)
    j2=math.radians(j2)
    d1=abs(w2-w1)
    d2=abs(j2-j1)
    h=hav(d1)+math.cos(w1)*math.cos(w2)*hav(d2)
    ans=math.asin(math.sqrt(h))*2*6371
    return ans
    pass

w1=float(input())
j1=float(input())
w2=float(input())
j2=float(input())
ans=Cal(w1,j1,w2,j2)
print("{:.4f}km".format(ans))
# Code By Phoenix_ZH
'''
34.260958
108.942369
55.755825
37.617298
'''

比率

img

import fractions
n=float(input())
n=int(n*10000000)
a=10000000#用于保护准确度
print(fractions.Fraction(n,10000000))
# Code By Phoenix_ZH

RGB转换HSV

img

R=float(input())/255
G=float(input())/255
B=float(input())/255
MAX,MIN=max(R,G,B),min(R,G,B)
V,S=MAX,(MAX-MIN)/MAX
if MAX==R:
    H=60*((MAX==R)*(0+((G - B)/(MAX-MIN))))
if MAX==G:
    H=60*(MAX==G)*(2+((B - R)/(MAX-MIN)))
if MAX==B:
    H=60*(MAX==B)*(4+(R-G)/(MAX-MIN))
# H=60*((MAX==R)*(0+((G - B)/(MAX-MIN)))+(MAX==G)*(2+((B - R)/(MAX-MIN)))+(MAX==B)*(4+(R-G)/(MAX-MIN)))
if(H<0):
    H+=360
print("%.4f"%H,"%.4f%%"%(S*100),"%.4f%%"%(V*100),sep=",")
# Code By Phoenix_ZH

指定精度舍入

img

f=float(input())
n=int(input())
print(round(f,n))

# Code By Phoenix_ZH

直角坐标转换为极坐标

img

import math
x=int(input())
y=int(input())
p=math.sqrt(x*x+y*y)
t=math.atan(y/x)
print("%.4f"%p,"%.4f"%t,sep=',')

# Code By Phoenix_ZH

计算狗的年龄

img

n=int(input())
ans=10.5*min(n,2)+4*max(n-2,0)
print(int(ans))

# Code By Phoenix_ZH

圆柱

img

import math
h=float(input())
r=float(input())
PI=math.acos(-1.0)
V=PI*r*r*h
S=PI*r*r*2+2*PI*r*h
print("%.4f"%V)
print("%.4f"%S)
# Code By Phoenix_ZH

风寒指数

img

import math
v=float(input())
T=float(input())
ans=13.12+0.6215*T-math.pow(v,0.16)*11.37+0.3965*T*math.pow(v,0.16)
print(round(ans))

# Code By Phoenix_ZH

两个分数的加、减、乘、除

img

import fractions
#By using fractions
a=int(input())
b=int(input())
c=int(input())
d=int(input())
s1=fractions.Fraction(a,c)
s2=fractions.Fraction(b,d)
a,c=s1.numerator,s1.denominator
b,d=s2.numerator,s2.denominator

print("(",a,"/",c,")","+","(",b,"/",d,")","=",s1+s2,sep="")
print("(",a,"/",c,")","-","(",b,"/",d,")","=",s1-s2,sep="")
print("(",a,"/",c,")","*","(",b,"/",d,")","=",s1*s2,sep="")
print("(",a,"/",c,")","/","(",b,"/",d,")","=",s1/s2,sep="")

# Code By Phoenix_ZH

勾股定理

img

import math
def isTriangle(a,b,c):
    if a+b > c and a+c > b and b+c > a and abs(a-b) < c and abs(a-c) < b and abs(c-b) < a:
        return True
    else:
        return False

a=int(input())
b=int(input())
c=int(math.sqrt(a*a+b*b))
c2=int(math.sqrt(abs(a*a-b*b)))
if(isTriangle(a, b, c)and(a*a+b*b==c*c)):
        print("c")
elif(isTriangle(a, b, c2)and((a*a+c2*c2==b*b)or(b*b+c2*c2==a*a))):
    if(c2==min(a,b,c2)):
        print('a')
    else:
        print('b')
else:
    print("non")
# Code By Phoenix_ZH

各位同学,创作不易,打赏一瓶饮料吧!
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Phoenix_ZengHao

创作不易,能否打赏一瓶饮料?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值