首页 > 试题广场 >

小红的函数最大值

[编程题]小红的函数最大值
  • 热度指数:1151 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红希望你求出函数 f(x)=log_a x-bx 在定义域上的最大值。你能帮帮她吗?

输入描述:
两个正整数a,b,用空格隔开
2\leq a \leq 1000
1\leq b \leq 1000


输出描述:
该函数在定义域上的最大值。你只需要保证和标准答案的相对误差不超过10^{-7}即可通过本题。
示例1

输入

2 1

输出

-0.9139286679
import math

# 读取输入的a和b
a, b = map(int, input().split())

# 求导数为0时的x值,即驻点
def find_stationary_point(a, b):
    return 1 / (b * math.log(a))

# 计算驻点处的函数值
def calculate_max_value(a, b):
    x = find_stationary_point(a, b)
    return (math.log(x) / math.log(a)) - b * x

# 计算并输出最大值
print(calculate_max_value(a, b))


编辑于 2025-03-04 12:00:04 回复(0)
import math
 
a, b = map(float, input().split())

x = 1.0 / (b * math.log(a))
res = math.log(x, a) - b * x

print("%.10f" % res)

编辑于 2025-03-12 19:24:27 回复(0)