类型:数值运算
描述
根据下面公式计算并输出x的值,a和b的值由用户输入,括号里的数字是角度值, 要求圆周率的值使用数学常数math.pi,开平方使用math库中开平方函数,三角函数的值用math库中对应的函数进行计算 。
输入格式
输入包括两行, 每行一个数字。
输出格式
表达式的值
示例
输入:
2
5
输出:
-0.9209814967618768
解析:
- 有两行输入,各用一个input()函数
- 每行输入一个数字,未说明数值类型时用浮点数float()
- 表达式需要转为python计算表达式
- 不能省略乘号
- 三角函数的参数应为弧度
- 分母需要加括号
- 若用pi,需import math
常见问题
- 导入函数时应该只导入函数名,不加括号
a=int(input())
b=int(input())
from math import sin(),cos()
x=(-b+(2*a*sin(1/6)*cos(1/6)**0.5))
print(x)
2. 三角函数参数应为弧度,角度要转为弧度
import math
c=math.sin(60)*math.cos(60)
c=2*a*c
c=c**0.5
a=(c-b)/(2*a)
print(a)
a=int(input())
b=int(input())
x=-b+2a*sin(60)*cos(60)**(1/2)/2*a
print(x)
a=float(input())
b=float(input())
import math
x=(-b+((2*a*math.sin(60)*math.cos(60))**0.5))/(2*a)
print(x)
import math
a = int(input())
b = int(input())
x = ((-b + (2 * a * math.sin(1/3) * math.cos(1/3)) ** 0.5) / (2 * a)) # 括号中参数应为弧度
print(x)
- 缺少乘号
a=int(input())
b=int(input())
x=-b+2a*sin(60)*cos(60)**(1/2)/2*a
print(x)
- 分母缺少括号
a=int(input())
b=int(input())
x=-b+2a*sin(60)*cos(60)**(1/2)/2*a
print(x)
import math
a=eval(input())
b=eval(input())
x=(-b+math.sqrt(2*a*math.sin(math.pi/3)*math.cos(math.pi/3)))/2*a
print(x)
题目未说明是整数时,输入应该转为浮点数
import math
a=int(input()) # float()
b=int(input())
x=(-b+(2*a*math.sin(math.pi/3)*math.cos(math.pi/3))**0.5)/(2*a)
print(x)
import math
a=int(input())
b=int(input())
c=2*a*math.sin(math.pi/3)*math.cos(math.pi/3)
d=c**0.5
x=(-b+d)/(2*a)
print(x)
import math
a=int(input()) # 浮点数
b=int(input())
x=(-b+(2*a*math.sin(math.pi/3)*math.cos(math.pi/3))**0.5)/(2*a)
print(x)
缺少输入语句 ```python import math
a=int(2) # a,b应由用户输入 b=int(5) print((-b+math.sqrt(2amath.sin(math.pi/3)math.cos(math.pi/3)) )/(2a))

```python
import math
c=math.sin(60)*math.cos(60)
c=2*a*c
c=c**0.5
a=(c-b)/(2*a)
print(a)
import math
x = (-b+(2*A*sin((math.pi)/3)*cos((math.pi)/3))**0.5)/(2*a) # A未定义
print(x)
定义太多变量名,程序可读性变差
import math
a=int(input())
b=int(input())
c=2*a*math.sin(math.pi/3)*math.cos(math.pi/3)
d=c**0.5
x=(-b+d)/(2*a)
print(x)
函数应用错误,此题应用正余弦函数
a=eval(input())
b=eval(input())
print(-b+math.pow(2*a*math.radians(60)*math.radians(60),0.5)/2/a)
角度转纸弧度出错
a=float(imput())
b=float(imput())
from math import sin,cos
x=(-b+(2*a*sin(1/6)*cos(1/6))**0.5)/2*a # 参数应为math.pi/3
print(x)
a=float(input())
b=float(input())
from math import *
x=(-b+(2*a*sin(60)*cos(60))**0.5)/(2*a)
print(x)
开平方处括号位置错 ```python a = eval(input()) b = eval(input()) import math print(-b + math.sqrt(2 a math.sin(math.pi / 3) math.cos(math.pi / 3) / (2 a)))
11. 重复用相同变量名导致混乱
```python
import math
c=math.sin(60)*math.cos(60)
c=2*a*c
c=c**0.5
a=(c-b)/(2*a)
print(a)
- 多种错误
13.括号位置错误
print()的括号到最后一个除号前结束,print()函数的返回值是None,结果导致None/(2*a),语法错误
import math
a=float(input())
b=float(input())
print(-b+(2*a*math.sin(math.pi/3)*math.cos(math.pi/3))**0.5)/(2*a) # print()的括号到最后一个除号前结束
# TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
- 不同计算方法会导致精度问题,请严格按题目描述调用正余弦函数计算,不要直接代入函数值
from math import sqrt
a=int(input())
b=int(input())
x=(-b+sqrt(2*a*sqrt(3)/2*0.5))/2/a
print(x)