forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLoan.cpp
More file actions
50 lines (45 loc) · 1.04 KB
/
AutoLoan.cpp
File metadata and controls
50 lines (45 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <set>
#include <queue>
#include <iterator>
#include <sstream>
#include <cassert>
#include <stack>
using namespace std;
/* AutoLoan http://community.topcoder.com/stat?c=problem_statement&pm=3970&rd=7993 */
double interestRate(double price, double monthlyPayment, int loanTerm)
{
double max = 100;
double min = 0;
double monthlyPercentage;
while (max - min > 1e-9)
{
double mid = min + (max - min) / 2;
int term = loanTerm;
double principal = price;
monthlyPercentage = (mid/principal)*(double)100;
for (int t = 0; t < term; ++t)
{
principal += (principal * monthlyPercentage)/(double)100;
principal -= (double)monthlyPayment;
}
if (principal < 1e-9)
min = mid;
else
max = mid;
}
return monthlyPercentage*(double)12;
}
int main(int argc, char *argv[])
{
cout<<interestRate(6800, 100, 68)<<endl;
cout<<interestRate(2000, 510, 4)<<endl;
cout<<interestRate(15000, 364, 48)<<endl;
getchar();
return 0;
}