計(jì)算代碼
class Solution {
public:
int getSum(int a, int b) {
double base = 2200000;//2160269.31;
double ratio = 0.04165;
int totalMonths = 360;
double monthlyPayout = base * (ratio/12) * pow((1 + ratio/12), totalMonths) / (pow((1 + ratio/12), totalMonths) - 1);
double totalPayout = monthlyPayout * totalMonths;
//cout << monthlyPayout <<endl;
int monthIndex = 1;
// 在第indexByAdvance個(gè)月決定還款
int indexByAdvance = 24;
bool flag = false;
// 提前還款的額度
double x = 200000;
// 假設(shè)當(dāng)前投資別的東西年收益率
double ratioByInvest = 0.045;
// 不提前還款,到第monthIndex個(gè)月為止的總利息
double sumInterest = 0;
// 不提前還款,到第monthIndex個(gè)月為止的剩余本金
double leftBase = base;
// 不提前還款剩余的總利息總和
double leftInterest = monthlyPayout*totalMonths - base;
// 提前還款剩余的總利息總和
// 提前還款(設(shè)在第K個(gè)月提前還款x元,K<monthIndex),到第monthIndex個(gè)月為止的總利息
double sumInterestByAdvance = 0;
// 提前還款(設(shè)在第K個(gè)月提前還款x元,K<monthIndex),到第monthIndex個(gè)月為止的剩余本金
double leftBaseByAdvance = base;
// x元做投資的總回報(bào)
double paybackByInvest = 0;
while(monthIndex <= totalMonths && leftBaseByAdvance >=0) {
/*
計(jì)算到第monthIndex個(gè)月為止的總支出(查看剩余本金和已經(jīng)支出的利息):
1. 假設(shè)不提前還款
(1)到第monthIndex個(gè)月為止的總利息 = monthlyPayout * monthIndex;
2. 在第K個(gè)月提前還款X元的總支出 = base - monthlyPayout * (monthIndex - k) - ()
*/
if (monthIndex == indexByAdvance) {
leftBaseByAdvance = leftBaseByAdvance - x;
}
if (monthIndex >= indexByAdvance) {
double cInterest = leftBaseByAdvance * (ratio/12);
double cBase = monthlyPayout - cInterest;
leftBaseByAdvance = leftBaseByAdvance - cBase;
sumInterestByAdvance = sumInterestByAdvance + cInterest;
flag = true;
// 假設(shè)拿x元做投資的回報(bào)金額是:
paybackByInvest = paybackByInvest + (x+paybackByInvest)*(ratioByInvest/12);
//cout<<"monthIndex: "<<monthIndex<<", paybackByInvest: "<<paybackByInvest<<endl;
}
double curInterest = leftBase * (ratio/12);
double curBase = monthlyPayout - curInterest;
leftBase = leftBase - curBase;
sumInterest = sumInterest + curInterest;
if (!flag) {
sumInterestByAdvance = sumInterest;
leftBaseByAdvance = leftBase;
}
// 作對(duì)比
/*
if ((totalPayout -base - sumInterest - paybackByInvest) > (monthlyPayout*333+x - base - sumInterestByAdvance) && (monthIndex >= indexByAdvance) ) {
cout<<"提前還款在第"<<monthIndex<<"個(gè)月開始奏效"<<endl;
}*/
if ((sumInterest-paybackByInvest) > sumInterestByAdvance) {
cout<<"提前還款在第"<<monthIndex<<"個(gè)月開始奏效"<<endl;
}
//cout<<monthlyPayout<<endl;
//cout<<sumInterest<<endl;
//cout<<monthIndex<<":"<<leftBaseByAdvance<<":"<<sumInterestByAdvance<<endl;
//cout<<monthIndex<<":"<<leftBase<<":"<<sumInterest<<endl;
cout<<"monthIndex: "<<monthIndex<<", sumInterest: "<<sumInterest - paybackByInvest<<", sumInterestByAdvance: "<<sumInterestByAdvance<<endl;
//cout<<"monthIndex: "<<monthIndex<<", paybackByInvest: "<<paybackByInvest<<", leftInterest: "<<totalPayout -base - sumInterest - paybackByInvest<<",leftBase: "<<leftBase<<", leftInterestInAdvance:"<<monthlyPayout*333+x - base - sumInterestByAdvance<<", leftBaseInAdvance: "<<leftBaseByAdvance<<endl;
monthIndex++;
//cout<<monthIndex<<endl;
}
return 1;
}
};
結(jié)論
似乎只要是投資利率高于還款利率就不虧。