Question:
I am trying to find the mathematical formula used in conjunction with this C++ code below. I am trying calculate by hand the solution be hand.I have found formulas involving logarithms but nothing below. and the solution they provide do not seem to match up with my answers. Any insight appreciated.
The code below is the solution to a ACM regional programming contest problem:
Using credit cards for your purchases is convenient, but they have high interest rates if you do not pay your balance in full each month. The interest rate is commonly quoted in terms of “annual percentage rate” (APR) which is then applied to the outstanding balance each month. The APR can be converted to a monthly interest rate R. At the end of each month, the monthly interest rate is applied to the outstanding balance and the interest is added to the total balance. Any payment made will be applied to the balance in the following month. The monthly interest is rounded to the nearest cent (rounding up 0.5 cent and above) in the calculations. You have unfortunately accumulated an outstanding balance B at the end of the month and you can only afford to pay up to some amount M every month. If you do not make any more purchases with the credit card, what is the minimum number of payments needed to completely eliminate the outstanding balance? It is possible that you cannot pay off the balance in 100 years (1200 payments). Input The input consists of multiple test cases. The first line of input is a single integer, not more than 1000,indicating the number of test cases to follow. Each of the following lines specify the input for one case. Each line contains three positive real numbers separated by single spaces: R, B, and M. The real numbers have two digits after the decimal point, satisfying R 50.00 and B,M 50000.00. R is the monthly interest rate and is specified as a percentage. Output For each case, display on a line the minimum number of payments needed to eliminate the outstanding balance. If this cannot be done in at most 1200 payments, print instead impossible.
Code:
#include <iostream>
using namespace std;
const double eps = 1e-8;
const double max_b = 50000;
const int max_payments = 1200;
int main()
{
int tn;
for (cin >> tn; tn--;)
{
double r, b, m;
cin >> r >> b >> m;
int minnum = 0;
double pb = max_b + 1;
while (b > 0 && minnum++ <= max_payments && b < pb)
{
pb = b;
b *= (1 + r / 100.0);
b = (int)(b * 100 + 0.5 + eps) / 100.0;
b -= m;
}
if (minnum > max_payments || b >= pb)
cout << "impossible" << endl;
else
cout << minnum << endl;
}
return 0;
}
============
Sample Input
11
2.00 100.00 105.00
2.00 100.00 102.00
2.00 100.00 100.00
2.00 100.00 4.00
2.00 100.00 3.00
2.00 100.00 1.00
2.00 100.00 2.00
9.56 5462.50 522.22
12.50 29876.44 33610.99
5.50 1.00 1.05
14.78 40181.09 46119.86
Sample Output
1
1
2
36
56
impossible
impossible
impossible
2
2
1