#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
float totalLoan( int year,int pricipal,double rate,int err = 500)
{
double monthfee = (double)pricipal/(12*year);
int lyear = 0;
int count = 0;
double actualtotal = 0.0;
double tmptotal = 0.0;
double totalrate = 0.0;
while(true)
{
if( count > 5000)
return -1.0;//quit
double totalloan = 0.0;
lyear = 0;
actualtotal = monthfee*12*year;
tmptotal = pricipal;
totalrate = 0.0;
for( lyear = 0 ; lyear < year*12;lyear++ )
{
totalrate += ( ( tmptotal )*(rate) ) /12.0 ;
tmptotal = tmptotal - monthfee + ( (tmptotal)*rate )/12.0;
//cout<<"lyear=="<<lyear<<"tmptotal=="<<tmptotal<<endl;
}
//cout<<"totalrate=="<<totalrate<<endl;
totalloan = totalrate + pricipal;
/*cout<<"totalloan=="<<totalloan<<endl;
cout<<"monthfee*12*year=="<<monthfee*12*year<<endl;
cout<<"err=="<<err<<endl;
cout<<"totalloan - monthfee*12*year=="<<totalloan - monthfee*12*year<<endl;
cout<<"monthfee*12*year - totalloan=="<<monthfee*12*year - totalloan<<endl;*/
if( abs( totalloan - monthfee*12*year) < err )
{
//cout<<"monthfee=="<<monthfee<<endl;
//cout<<"totalfee=="<<monthfee*12*year<<endl;
return monthfee*12*year;
}
if( totalloan - monthfee*12*year > err )
{
monthfee += (totalloan - monthfee*12*year)/( 12*year );
//cout<<"monthfee++=="<<monthfee<<endl;
}else if( totalloan - monthfee*12*year < err )
{
monthfee += (totalloan - monthfee*12*year)/( 12*year );
//cout<<"monthfee--=="<<monthfee<<endl;
}
count++;
}
}
void SummerFunction(int totalbackfee)
{
int initbigFee = 0;
double Alltotal = 0.0;
double pretotal = 0.0;
int tag = 0;
int finalfee = 0;
for( initbigFee = 10000 ; initbigFee <= totalbackfee ; initbigFee += 10000 )
{
int bigyear = 15;
int bigfee = 280000 - initbigFee>0?280000 - initbigFee:0;
double rate = 0.049;
double bigtotal = totalLoan(bigyear,bigfee,rate);
int smallyear = 10;
int smallfee = 150000 - (totalbackfee - initbigFee) > 0?150000 - (totalbackfee - initbigFee):0;
double smalltotal = totalLoan(smallyear,smallfee,rate);
if( tag == 0 )
{
pretotal = bigfee + smallfee;
tag = 1;
}
Alltotal = bigtotal + smalltotal;
if( pretotal > Alltotal )
{
finalfee = initbigFee;
pretotal = Alltotal;
}
cout<<"总还贷"<<totalbackfee<<"大套还"<<initbigFee<<"总共还贷=="<<Alltotal<<"大套还贷=="<<bigtotal<<"小套还贷=="<<smalltotal<<endl;
}
cout<<"-------------------"<<endl;
cout<<"最优还贷是大套还"<<finalfee<<endl;
cout<<"这时公共还贷是=="<<pretotal<<endl;
//totalLoan(15,30,0.0325);
cout<<"公积金还"<<totalLoan(15,300000,0.0325)<<endl;
}
int main()
{
int fee = 250000;
//cout<<""
//cin<<fee;
SummerFunction(fee);
return 0;
}