873 - Loan (II)

All about problems in Volume 8. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
mortalhas
New poster
Posts: 1
Joined: Tue Mar 15, 2005 4:36 pm

873 - Loan (II)

Post by mortalhas »

can somebody tell me how can I calculate the output of the program
I only need the formula, nothing else.
I just can't seem to find the formula

HEELPPPPPPP :cry:

luckybigpack
New poster
Posts: 1
Joined: Thu Jun 16, 2005 5:52 pm

about the output format...

Post by luckybigpack »

Hi there,
I tried to submit the Code (C++) for problem 873 and the compilation worked well, but then i got a "wrong answer".

I know that my results are right so it has to be something with the output format which is causing the error.

Now my questions:
Should my Output come with a leading blank line and or a blank line at the end?
What Output should I have if the numbers of cases are 0 or negative? None? Blank line?

And as I am a newbie to c++ could you tell me if my Code (just in/output) is wrong? It works fine using the console, but I actually don't know how the input comes from the judge or how he is reading the output...

Code: Select all

#include <iostream>
#include <vector>

using namespace std;


int main () {   
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    int cases = 0;
    
    cin >> cases;

    if (cases <= 0) {
        return 0;
    }    
    int initial;
    int percentage;
    int months;
    vector<float> result(cases);
    
    for (int i=0;i<cases;i++) {
        cin >> initial >> percentage >> months;
        result[i] = monthly(initial, percentage, months);
    } 
     
    if (cases == 1) {
        cout << result[cases-1];
        return 0;
    }  
      
    for (int i = 0; i < cases-1; i++) {
        cout << result[i] << endl << endl;
    }  
         
    cout << result[cases-1];
    
    return 0;
}
PS: It really would have helped if the code samples postet in "HOWTOs" would have worked...

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Re: about the output format...

Post by Martin Macko »

luckybigpack wrote:Should my Output come with a leading blank line and or a blank line at the end?
You should write endl ("\n") after every line.

Code: Select all

    cout << result[cases-1] << endl;

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon »

Hi,

You can find the formula at
http://mathforum.org/dr.math/faq/faq.interest.html

Then scroll down to the topic titled
"Installment Loans"

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

I don't know why but I got WA. I think is a precision error. Could someone give me any critical I/O?

Thx in advance :wink:

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon »

Hi,

If you are using the formula then the only critical IO that comes to mind is when the interest is 0%. In this case, the answer is just

INITIAL_CAPITAL / MONTHS

I tried bi-section method but got TLE. Any other kind of method is beyond me.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

There is a O(1) formula, and it's very easy to derive:

Let a_i be the amount of money you owe the bank at the end of i-th month, a_0 = initial capital, Y = yearly interest, and P be the monthly payment.

Clearly, a_i = a_{i-1} + Y/12 a_{i-1} - P.
You can write this recurrence in the form: a_i = r a_{i-1} - P, where r = 1 + Y/12.

First few elements of {a_i} are:
a_1 = r a_0 - P
a_2 = r^2 a_0 - r P - P
a_3 = r^3 a_0 - r^2 P - r P - P
a_4 = r^4 a_0 - r^3 P - r^2 P - r P - P
...
a_n = r^n a_0 - P(1 + r + r^2 + ... + r^(n-1))

The sum in brackets is a geometric series, so it simplifies to:
a_n = r^n a_0 - P (r^n - 1) / (r - 1)

Knowing that a_n = 0, you can solve for P in the last equation and that's all.

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

Thx a lot daveon that was my mistake!!!! :D

Best regards

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon »

No problem Antonio and thanks again Max!

Can't believe I didn't see it.
I better work more on my math than on programming. :oops:

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

I use bisection and get WA.. is there any I/O?

I run the bisect 200 times, so it should be precise enough..

Post Reply

Return to “Volume 8 (800-899)”