1.输入两个数值, 求两个数的最大公约数和最小公倍数.
def count(a, b):
if a < b:
a, b = b, a
while b != 0:
multiple = a % b
a = b
b = multiple
return a
a = int(input('请输入第一个数:'))
b = int(input('请输入第二个数:'))
multiple = count(a, b)
divisor = (a * b) // multiple
print('最小公倍数是:', multiple)
print('最大公倍数是:', divisor)
2.计算并返回x的平方根,其中x是非负数,结果保留整数
import math
num = int(input('请输入要开平方的数:'))
if num < 0:
print('必须是非负数!')
else:
a = math.sqrt(num)
a = a // 1
print('%.0f' % a)
输入三个数,求一元二次方程ax**2 + bx +c = 0的解
import math
a = int(input('a:'))
b = int(input('b:'))
c = int(input('c:'))
if a == 0:
print("a不能为0")
else:
y = b ** 2 - 4 * a * c
if y < 0:
print("无解")
elif y == 0:
print("一个解:")
x = (-b + math.sqrt(y)) / (2 * a)
print('x = ', x)
else:
print("两个解:")
x1 = (-b + math.sqrt(y)) / (2 * a)
x2 = (-b - math.sqrt(y)) / (2 * a)
print('x1 = ', x1)
print('x2 = ', x2)