10114 - Loansome Car Buyer

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

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10114 - Loansome Car Buyer

If a month is not listed, then the previous depreciation percentage applies.

Code: Select all

``````month = 0, depreciation = 0.10, value = 67500.00, owe = 75000.00
month = 1, depreciation = 0.10, value = 60750.00, owe = 74250.00
month = 2, depreciation = 0.10, value = 54675.00, owe = 73500.00
month = 3, depreciation = 0.10, value = 49207.50, owe = 72750.00
month = 4, depreciation = 0.10, value = 44286.75, owe = 72000.00
month = 5, depreciation = 0.10, value = 39858.08, owe = 71250.00
month = 6, depreciation = 0.10, value = 35872.27, owe = 70500.00
month = 7, depreciation = 0.10, value = 32285.04, owe = 69750.00
month = 8, depreciation = 0.10, value = 29056.54, owe = 69000.00
month = 9, depreciation = 0.10, value = 26150.88, owe = 68250.00
month = 10, depreciation = 0.10, value = 23535.79, owe = 67500.00
month = 11, depreciation = 0.10, value = 21182.22, owe = 66750.00
month = 12, depreciation = 0.10, value = 19063.99, owe = 66000.00
month = 13, depreciation = 0.10, value = 17157.59, owe = 65250.00
month = 14, depreciation = 0.10, value = 15441.83, owe = 64500.00
month = 15, depreciation = 0.10, value = 13897.65, owe = 63750.00
month = 16, depreciation = 0.10, value = 12507.89, owe = 63000.00
month = 17, depreciation = 0.10, value = 11257.10, owe = 62250.00
month = 18, depreciation = 0.10, value = 10131.39, owe = 61500.00
month = 19, depreciation = 0.10, value = 9118.25, owe = 60750.00
month = 20, depreciation = 0.10, value = 8206.42, owe = 60000.00
month = 21, depreciation = 0.10, value = 7385.78, owe = 59250.00
month = 22, depreciation = 0.10, value = 6647.20, owe = 58500.00
month = 23, depreciation = 0.10, value = 5982.48, owe = 57750.00
month = 24, depreciation = 0.10, value = 5384.23, owe = 57000.00
month = 25, depreciation = 0.10, value = 4845.81, owe = 56250.00
month = 26, depreciation = 0.10, value = 4361.23, owe = 55500.00
month = 27, depreciation = 0.10, value = 3925.11, owe = 54750.00
month = 28, depreciation = 0.10, value = 3532.60, owe = 54000.00
month = 29, depreciation = 0.10, value = 3179.34, owe = 53250.00
month = 30, depreciation = 0.10, value = 2861.40, owe = 52500.00
month = 31, depreciation = 0.10, value = 2575.26, owe = 51750.00
month = 32, depreciation = 0.10, value = 2317.74, owe = 51000.00
month = 33, depreciation = 0.10, value = 2085.96, owe = 50250.00
month = 34, depreciation = 0.10, value = 1877.37, owe = 49500.00
month = 35, depreciation = 0.10, value = 1689.63, owe = 48750.00
month = 36, depreciation = 0.10, value = 1520.67, owe = 48000.00
month = 37, depreciation = 0.10, value = 1368.60, owe = 47250.00
month = 38, depreciation = 0.10, value = 1231.74, owe = 46500.00
month = 39, depreciation = 0.10, value = 1108.57, owe = 45750.00
month = 40, depreciation = 0.10, value = 997.71, owe = 45000.00
month = 41, depreciation = 0.10, value = 897.94, owe = 44250.00
month = 42, depreciation = 0.10, value = 808.14, owe = 43500.00
month = 43, depreciation = 0.10, value = 727.33, owe = 42750.00
month = 44, depreciation = 0.10, value = 654.60, owe = 42000.00
month = 45, depreciation = 0.10, value = 589.14, owe = 41250.00
month = 46, depreciation = 0.10, value = 530.22, owe = 40500.00
month = 47, depreciation = 0.10, value = 477.20, owe = 39750.00
month = 48, depreciation = 0.10, value = 429.48, owe = 39000.00
month = 49, depreciation = 0.10, value = 386.53, owe = 38250.00
month = 50, depreciation = 0.10, value = 347.88, owe = 37500.00
month = 51, depreciation = 0.10, value = 313.09, owe = 36750.00
month = 52, depreciation = 0.10, value = 281.78, owe = 36000.00
month = 53, depreciation = 0.10, value = 253.60, owe = 35250.00
month = 54, depreciation = 0.10, value = 228.24, owe = 34500.00
month = 55, depreciation = 0.10, value = 205.42, owe = 33750.00
month = 56, depreciation = 0.10, value = 184.88, owe = 33000.00
month = 57, depreciation = 0.10, value = 166.39, owe = 32250.00
month = 58, depreciation = 0.10, value = 149.75, owe = 31500.00
month = 59, depreciation = 0.10, value = 134.78, owe = 30750.00
month = 60, depreciation = 0.10, value = 121.30, owe = 30000.00
month = 61, depreciation = 0.10, value = 109.17, owe = 29250.00
month = 62, depreciation = 0.10, value = 98.25, owe = 28500.00
month = 63, depreciation = 0.10, value = 88.43, owe = 27750.00
month = 64, depreciation = 0.10, value = 79.58, owe = 27000.00
month = 65, depreciation = 0.10, value = 71.63, owe = 26250.00
month = 66, depreciation = 0.10, value = 64.46, owe = 25500.00
month = 67, depreciation = 0.10, value = 58.02, owe = 24750.00
month = 68, depreciation = 0.10, value = 52.21, owe = 24000.00
month = 69, depreciation = 0.10, value = 46.99, owe = 23250.00
month = 70, depreciation = 0.10, value = 42.29, owe = 22500.00
month = 71, depreciation = 0.10, value = 38.06, owe = 21750.00
month = 72, depreciation = 0.10, value = 34.26, owe = 21000.00
month = 73, depreciation = 0.10, value = 30.83, owe = 20250.00
month = 74, depreciation = 0.10, value = 27.75, owe = 19500.00
month = 75, depreciation = 0.10, value = 24.97, owe = 18750.00
month = 76, depreciation = 0.10, value = 22.48, owe = 18000.00
month = 77, depreciation = 0.10, value = 20.23, owe = 17250.00
month = 78, depreciation = 0.10, value = 18.21, owe = 16500.00
month = 79, depreciation = 0.10, value = 16.39, owe = 15750.00
month = 80, depreciation = 0.10, value = 14.75, owe = 15000.00
month = 81, depreciation = 0.10, value = 13.27, owe = 14250.00
month = 82, depreciation = 0.10, value = 11.95, owe = 13500.00
month = 83, depreciation = 0.10, value = 10.75, owe = 12750.00
month = 84, depreciation = 0.10, value = 9.68, owe = 12000.00
month = 85, depreciation = 0.10, value = 8.71, owe = 11250.00
month = 86, depreciation = 0.10, value = 7.84, owe = 10500.00
month = 87, depreciation = 0.10, value = 7.05, owe = 9750.00
month = 88, depreciation = 0.10, value = 6.35, owe = 9000.00
month = 89, depreciation = 0.10, value = 5.71, owe = 8250.00
month = 90, depreciation = 0.10, value = 5.14, owe = 7500.00
month = 91, depreciation = 0.10, value = 4.63, owe = 6750.00
month = 92, depreciation = 0.10, value = 4.16, owe = 6000.00
month = 93, depreciation = 0.10, value = 3.75, owe = 5250.00
month = 94, depreciation = 0.10, value = 3.37, owe = 4500.00
month = 95, depreciation = 0.10, value = 3.04, owe = 3750.00
month = 96, depreciation = 0.10, value = 2.73, owe = 3000.00
month = 97, depreciation = 0.10, value = 2.46, owe = 2250.00
month = 98, depreciation = 0.10, value = 2.21, owe = 1500.00
month = 99, depreciation = 0.50, value = 1.11, owe = 750.00
month = 100, depreciation = 0.50, value = 0.55, owe = 0.00
100 months
``````
Check input and AC output for thousands of problems on uDebug!

Morag2
New poster
Posts: 2
Joined: Sat Dec 14, 2013 4:26 am

Re: 10114 - Loansome Car Buyer

So I've just returned to UVA problems... and while this looks simple, I am doing something wrong. It is probably silly, but I'd appreciate another pair of eyes. Here is my code - it works on the test cases in this topic.

Code: Select all

``````#include <iostream>

using namespace std;

int main(){

int months, numRecords;
double down, cvalue;
double records[101][2]; //Stores the depreciation information

bool first = true;

while((cin >> months >> down >> cvalue >> numRecords) && months >= 0){

for(int i = 0; i < numRecords; i++){
cin >> records[i][0] >> records[i][1];
}

double owed = cvalue;
double curDep = records[0][1];
double value = (cvalue + down) * (1 - curDep);
double paidMonthly = owed/months;
int posDep = 1; //Where we are in the depreciation records

//Forgot to check if it is already true in month 0
if(owed < value){
cout << "0 months" << endl;
break;
}

int i;
for(i = 1; i <= months; i++){
if(records[posDep][0] == i){
curDep = records[posDep][1];
posDep++;
}

owed -= paidMonthly;
value *= (1 - curDep);

if(owed < value){
cout << i << (i == 1 ? " month" : " months") << endl;
break;
}
}

}

return 0;
}``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10114 - Loansome Car Buyer

Input:

Code: Select all

``````30 500.0 15000.0 3
0 .10
1 .03
3 .002
12 500 9999.99 2
0 .05
2 .1
60 2400.0 30000.0 3
0 .2
1 .05
12 .025
100 0 60000 101
0 .1
1 .1
2 .01
3 .01
4 .1
5 .1
6 .01
7 .01
8 .01
9 .01
10 .1
11 .01
12 .01
13 .01
14 .01
15 .01
16 .01
17 .01
18 .01
19 .01
20 .1
21 .01
22 .01
23 .01
24 .01
25 .01
26 .01
27 .01
28 .01
29 .01
30 .1
31 .01
32 .01
33 .01
34 .01
35 .01
36 .01
37 .01
38 .01
39 .01
40 .1
41 .01
42 .01
43 .01
44 .01
45 .01
46 .01
47 .01
48 .01
49 .01
50 .1
51 .01
52 .01
53 .01
54 .01
55 .01
56 .01
57 .01
58 .01
59 .01
60 .1
61 .01
62 .01
63 .01
64 .01
65 .01
66 .01
67 .01
68 .01
69 .01
70 .1
71 .1
72 .1
73 .1
74 .1
75 .01
76 .01
77 .01
78 .01
79 .01
80 .1
81 .1
82 .1
83 .1
84 .1
85 .1
86 .1
87 .1
88 .1
89 .1
90 .1
91 .1
92 .1
93 .01
94 .01
95 .01
96 .1
97 .1
98 .1
99 .1
100 .1
100 0 75000 2
0 .10
99 .5
100 74999 1 1
0 .20
36 2000 10000 4
0 .2
1 .1
3 .01
10 .005
36 0 12000 3
0 .25
1 .1
10 .005
23 3000 5000 2
0 .15
20 .005
42 4000 0 1
0 .79
-10 0 0 4
``````
AC output:

Code: Select all

``````4 months
1 month
49 months
99 months
100 months
0 months
11 months
27 months
0 months
0 months
``````
Check input and AC output for thousands of problems on uDebug!

Morag2
New poster
Posts: 2
Joined: Sat Dec 14, 2013 4:26 am

Re: 10114 - Loansome Car Buyer

Thanks, it was indeed a stupid mistake - I was exiting the main loop anytime the answer was 0 months.

mahade hasan
Learning poster
Posts: 87
Joined: Thu Dec 15, 2011 3:08 pm
Location: University of Rajshahi,Bangladesh

Re: 10114 - Loansome Car Buyer

getting WAAA
i gives right ans all the test case above
but still waaa
plz help

Code: Select all

``````#include<stdio.h>

int main()
{
long I,K,L,M,N,Month;
float Current,Loan,Down,Car,Check;

while(true){
scanf("%ld %f %f %ld",&Month,&Down,&Loan,&N);
if(Month<0) break;
float Value[1000]={0.0};

while(N--){
scanf("%ld %f",&I,&Current);
Value[I]=Current;
}
Check=(float)(Loan/Month);
Car=Loan;
Loan+=Down;
Loan=Loan-(Loan*Value[0]);
//printf("here Loan=%f\n",Loan);
Current=Value[0];
I=0;
if(Car>=Loan)
for(I=1;;I++){
if(Value[I]>0.0){
//printf("here Value=%f  Loan=%f I=%ld\n",Value[I],Loan,I);
Current=Value[I];
Loan=Loan-(Loan*Value[I]);
}
else Loan=Loan-(Loan*Current);

Car-=Check;
//printf("month=%ld deception=%f value=%f owe=%f\n",I,Current,Loan,Car);
if(Car<Loan) break;
}
if(I==1)
printf("1 month\n");
else if(I==0)
printf("0 month\n");
else printf("%ld months\n",I);
}
return 0;
}

``````
[/color]
we r surrounded by happiness
need eyes to feel it!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10114 - Loansome Car Buyer

Try using double instead of float.
Check input and AC output for thousands of problems on uDebug!

Blief.S
New poster
Posts: 16
Joined: Wed Jun 18, 2014 9:03 pm

Re: 10114 - Loansome Car Buyer

What is the problem in this code plz.....some test cases didn't pass..... got no idea why??

Code: Select all

``````#include <stdio.h>
int main()
{
int month,cas,n,i,j,mon[1000];
float down,loan,owe,worth,dep[1000],instlmnt;
while(scanf("%d %f %f %d",&month,&down,&loan,&cas)){
if(month<0) break;
n=0;
owe=0;
worth=0;
instlmnt = loan/month;
for(i=0;i<cas;i++)
scanf("%d %f",&mon[i],&dep[i]);
mon[i] = month;
owe = loan+down;
worth = loan;
int k=0;
j=0;
while(j<month){
for(i=0;i<mon[k+1]-mon[k];i++){
owe = owe - (owe*dep[k]);
worth = worth - instlmnt;
n = n + 1;
if (owe > worth) break;

}
k++;
j=j+i;
if (owe > worth) break;

}
if (j == 1) printf("1 month\n");
else printf ("%d months\n",j);
}
return 0;
}
``````

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 10114 - Loansome Car Buyer

Blief.S wrote:What is the problem in this code plz.....some test cases didn't pass..... got no idea why??
I'd suggest you start by taking the time to read the previous messages in this thread. For example, the message just above yours has good advice that applies to your code.

apcastelein
New poster
Posts: 15
Joined: Wed Jul 23, 2014 12:57 am

Re: 10114 - Loansome Car Buyer

I've tried all the test cases given in this thread and I get the right answer for all of them. When I submit my code I get a runtime erorr. I've checked it for array index out of bounds and division by zero and I'm pretty sure those errors can't happen.

Code: Select all

``````#include <stdio.h>

int main(){
int borMonths;
double totLoan,downPayment;
int numData;
int mIndex,i;
double carValue,loanLeft,payPerMonth;
scanf("%d %lf %lf %d\n",&borMonths,&downPayment,&totLoan,&numData);
while(borMonths>0){
//init values
payPerMonth=totLoan/borMonths;
int markedMonths[numData];
double depPercent[numData];
carValue=totLoan+downPayment;
loanLeft=totLoan;
//scan in depreciation percents for months
i=0;
while(i<numData){
scanf("%d %lf\n",&markedMonths[i],&depPercent[i]);
i++;
}
//run first iteration
i=0;
carValue*=1-depPercent[i];
mIndex=0;
//run other iterations till car value< loan left
while(loanLeft>=carValue){
mIndex++;
if(i+1!=numData && markedMonths[i+1]<=mIndex)
i++;
loanLeft-=payPerMonth;
carValue*=1-depPercent[i];
}
printf("%d %s\n",mIndex,(mIndex==1)? "month" : "months");
scanf("%d %lf %lf %d\n",&borMonths,&downPayment,&totLoan,&numData);
}
return 1;
}
``````

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 10114 - Loansome Car Buyer

apcastelein wrote:I've tried all the test cases given in this thread and I get the right answer for all of them. When I submit my code I get a runtime erorr. I've checked it for array index out of bounds and division by zero and I'm pretty sure those errors can't happen.
Don't return 1 from main. It should return 0.

I think most online judges follow POSIX conventions regarding the exit codes of programs. If you're not familiar with this, try googling for "exit status" (for example, see the second paragraph from this document).

apcastelein
New poster
Posts: 15
Joined: Wed Jul 23, 2014 12:57 am

Re: 10114 - Loansome Car Buyer

Ahh I see. I did learn to return 0, but I'm still new to C++ so I guess it hasn't completely sunk in yet. Thanks for the help, I really appreciate it.

Muftee_Ruet
New poster
Posts: 10
Joined: Fri Sep 16, 2011 6:36 am

Re: 10114 - Loansome Car Buyer

In the problem statement
For each loan, the output is the number of complete months before the borrower owes less than the car is worth. Note that English requires plurals (5 months) on all values other than one (1 month).
does that mean i have to print
0 months
instead of
0 month
when the answer is 0?

apcastelein
New poster
Posts: 15
Joined: Wed Jul 23, 2014 12:57 am

Re: 10114 - Loansome Car Buyer

Correct.

When I was trying to figure out the problem I came across this article, and that is one of the things which messed up his algorithm

http://denny518.blogspot.com/2013/12/ac ... e-car.html

sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

Re: 10114 - Loansome Car Buyer

got AC,thanks
Last edited by sampad74 on Fri Oct 03, 2014 8:23 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10114 - Loansome Car Buyer

That is AC code.
Check input and AC output for thousands of problems on uDebug!