使用二等分搜索使该程序在一年内更快地用信用卡付款

时间:2019-02-10 04:16:01

标签: python-3.x bisection

我想知道我的代码有什么问题。我已经划分了上限和下限。我每次都失去一分钱。

我分裂了上限。

def get_credit_exact_pay(balance, annualInterestRate):
    monthly_interest_rate = round(annualInterestRate/12, 2)
    upper_x = balance
    lower_x = balance
    lower_payment = round(balance/12,2)
    upper_payment = round((balance * (1 + monthly_interest_rate) ** 12) / 12, 2)

我认为我在计算付款或付款增量时还缺少关于数学的一件事。

    count = 0
    while True:
        upper_x -= upper_payment
        interest = (annualInterestRate/12.0) * upper_x
        upper_x += interest

        lower_x -= lower_payment
        interest = (annualInterestRate/12.0) * lower_x
        lower_x += interest

        if count == 11:
            if lower_x < lower_payment and lower_x > 0:
                print('l')
                print ("%.2f" % lower_payment)
                break
            if upper_x < upper_payment and upper_x > 0:
                print ("%.2f" % upper_payment)
                break
            count = 0
            upper_x = balance
            lower_x = balance
            upper_payment -= .01
            lower_payment += .01
            continue
        else:
            if lower_x < 0:
                lower_x = balance/2
            if upper_x < 0:
                upper_x = balance/2
            count += 1
            continue

0 个答案:

没有答案