试图进行二分搜索工作但是

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图进行二分搜索工作但是相关的知识,希望对你有一定的参考价值。

我正在关注麻省理工学院开放式课程6.0001,在问题集1部分c中,我们需要设置二分搜索,以便找到最佳的储蓄率,但我的代码没有像我预期的那样工作。需要帮助才能找出问题所在。

total_cost = float(input("Cost of dream house"))
down_payment = total_cost * 0.25
annual_salary = float(input("Annual salary"))
monthly_salary = annual_salary / 12
current_savings = 0

x = 100.0000
low = 0.0000
high = 1.0000
guess = (high + low) / 2

r = 0.04
portion_saved = monthly_salary * guess
semi_annual_raise = (100 + 100*(float(input("Semi annual raise")))) / 100
number_of_months = 0.00
max_number_of_months = float(input("Max Time (in months)"))

while abs(current_savings - down_payment) > x:
    if current_savings - down_payment > x:
        high = guess
        guess = (high + low) / 2
        portion_saved = monthly_salary * guess
        current_savings = 0
    elif down_payment - current_savings > x:
        low = guess
        guess = (high + low) / 2
        portion_saved = monthly_salary * guess
        current_savings = 0
    else:
        guess = guess
        portion_saved = monthly_salary * guess

    if number_of_months < max_number_of_months and number_of_months % 6 == 0:
        current_savings *= (100 + r / 12) / 100
        portion_saved *= semi_annual_raise
        current_savings += portion_saved
        number_of_months += 1
    elif number_of_months < max_number_of_months and number_of_months % 6 != 0:
        current_savings *= (100 + r / 12) / 100
        current_savings += portion_saved
        number_of_months += 1


print(current_savings)
print(number_of_months)

预期结果:第一个if else语句提供了在第二个if else语句中使用的guess值,如果它是abs(current_savings - downpayment)> x,则循环再次运行直到abs(current_savings - downpayment)<x。实际结果:程序在第一个if语句中陷入无限循环。

答案
    """ x = current_savings y = portion_saved z = number_of_months  """
    while x < 250000:
        if z >= 1 and z % 6 == 0:
            x *= (100 + 4 / 12) / 100
            y *= 1.07
            x += y
            z += 1

        else:
            x *= (100 + 4 / 12) / 100
            x += y
            z += 1
    L1 = [x, z]
    return L1


def rate(a, r_rate, lo, hi):
    """ a = number_of_months"""
    if a - 36 > 0:
        lo = r_rate
        r_rate = (hi + lo) / 2.0
        L2 = [r_rate, lo]
        return L2
    else:
        hi = r_rate
        r_rate = (hi + lo) / 2.0
        L3 = [r_rate, hi]
        return L3


total_cost = 1000000
down_payment = total_cost * 0.25
annual_salary = int(input("Annual Salary"))
monthly_salary = annual_salary / 12
current_savings = 0
number_of_months = 0
low = 0.0
high = 1.0
r = 0.5
num_tries = 0

while abs(current_savings - down_payment) > 100:
    portion_saved = monthly_salary * r
    current_savings = 0
    number_of_months = 0
    L5 = [savings(current_savings, portion_saved, number_of_months)[0], savings(current_savings, portion_saved, number_of_months)[1]]
    current_savings = L5[0]
    number_of_months = L5[1]
    L6 = [(rate(number_of_months, r, low, high)[0]), (rate(number_of_months, r, low, high)[1])]
    r = L6[0]
    if number_of_months - 36 > 0:
        low = L6[1]

    else:
        high = L6[1]
    num_tries += 1


print(r)
print(num_tries)

以上是关于试图进行二分搜索工作但是的主要内容,如果未能解决你的问题,请参考以下文章

谷歌丰富的片段不工作

了解二分搜索错误

Android - 将搜索传递给片段

Python算法-冒泡排序、线性和二分搜索

Android 应用程序片段不断崩溃

在我的“设置”片段中膨胀类 PreferenceScreen 时出错