我正在尝试解决TopCoder上的Mortgage问题。我的代码工作得很好 - 它通过了除最后一个之外的每个系统测试,而不是“1976284585”它返回“1976284584”。我试图解决这个问题,但我不能这样做。你能救我吗?
我的源代码:
#include<iostream>
#include<cmath>
using namespace std;
class Mortgage{
public:
int monthlyPayment(int loan, int interest, int term){
long double lo = 0;
long double hi = 2000000000;
long double monthlyPayment;
long double balance;
long double ir = interest/10.0/12.0/100.0;
while((hi-lo)/hi>1e-9){
balance = loan;
monthlyPayment = lo+(hi-lo)/2.0;
for(int i=0;i<term*12;i++){
balance = balance-monthlyPayment;
if(balance<=0.0){break;}
balance = ceil(balance*(1+ir));
}
if(balance<=0.0){
hi=monthlyPayment;
}else{
lo=monthlyPayment+1;
}
}
return lo;
}
};