什么是计算和二等分的正确结构

问题描述

我一直在解决该问题,因为“编写一个使用这些范围和二等分搜索的程序(有关更多信息,请查看二等分搜索的Wikipedia页面),以找到最低的月度支付百分比(没有$ 10的倍数) ),以便我们可以在一年内还清债务。”两天。但是,我的代码无法持续通过评分者的测试。上面提到的界限是:

Monthly interest rate = (Annual interest rate) / 12.0
Monthly payment lower bound = Balance / 12
Monthly payment upper bound = (Balance x (1 + Monthly interest rate)12) / 12.0

我在许多网站上搜索了很多代码,但是,即使作者说的是正确的,每个代码也没有通过测试。我将在下面发布我的代码,希望您能告诉我我的代码有什么问题?测试是:

Test 1: balance=320000 annunlInterestRate=0.2 Lowest payment=29157.09   My output:29591.88
Test 2: balance=999999 annunlInterestRate=0.18 Lowest payment=90325.03  My output:92474.54

期待您的帮助。

balance =balance
annualInterestRate=0.2
monthlyInterestRate = annualInterestRate/12
monthly_paymentupperbound =round(((balance * (1 + monthlyInterestRate)**12) / 12.0),2)
monthly_paymentlowerbound = round((balance / 12),2)
epsilon = 0.01

lowest_payment=(monthly_paymentupperbound+monthly_paymentlowerbound)/2

while abs(lowest_payment-monthly_paymentupperbound)<=epsilon:
    for i in range(12):
        balance=round((balance-lowest_payment),2)
        interest=round((balance*monthlyInterestRate),2)
        balace=round((balance + interest),2)
    if balance==0:
        print('Lowest Payment:',round(lowest_payment,2))
        break
                      
    else:
        if lowest_payment< monthly_paymentupperbound:
            monthly_paymentlowererbound = lowest_payment
            
        else:
            monthly_paymentupperbound=lowest_payment
    
        
            
lowest_payment=round((monthly_paymentupperbound+monthly_paymentlowerbound)/2,2)  
      
print('Lowest Payment:',2)) 

我尝试如下改进我的代码,但现在没有任何输出

balance = 999999.00
annualInterestRate=0.18
monthlyInterestRate = annualInterestRate/12
monthly_paymentupperbound =(balance * (1 + monthlyInterestRate)**12) / 12.0
monthly_paymentlowerbound = balance / 12
epsilon = 0.1

lowest_payment=(monthly_paymentupperbound+monthly_paymentlowerbound)/2
while True:
    if balance !=0:
       for i in range(12):
    
          lowest_payment=(monthly_paymentupperbound + monthly_paymentlowerbound)/2
          balance=(1+monthlyInterestRate)*balance-lowest_payment
          
       while abs(lowest_payment-monthly_paymentupperbound)>=epsilon: 
              
               if lowest_payment< monthly_paymentupperbound:
                   monthly_paymentlowererbound = lowest_payment
                   
               else:
                   monthly_paymentupperbound=lowest_payment
                  

               lowest_payment=round((monthly_paymentupperbound+monthly_paymentlowerbound)/2,2)
               balance=(1+monthlyInterestRate)*balance-lowest_payment
    else:
          break
        
     
  
      
print('Lowest Payment:',2))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)