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

samin
New poster
Posts: 6
Joined: Fri Jul 18, 2008 9:29 pm

Re: 10114 - Loansome Car Buyer

Post by samin » Mon Nov 16, 2015 11:04 am

HI,

can anyone please tell me why is my java code showing compilation error while it is working on java online editors?

Code: Select all

import java.io.*;
import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        Scanner in = new Scanner(System.in);
      	//PrintWriter out = new PrintWriter(System.out);
        while(true)
        {
            int year = in.nextInt();
          	if(year < 0)
          	    break;
          	float dPay1 = in.nextFloat();
          	float loan = in.nextFloat();
          	int nDep = in.nextInt();
          	int[] years = new int[nDep];
          	float[] values = new float[nDep];
          	float finalVal = dPay1 + loan;
          	
          	float installment = loan / year;
          	
          	for(int i = 0; i < nDep; i++ )
          	{
          		int temp1 = in.nextInt();
          		years[i] = temp1;
          		float temp2 = in.nextFloat();
          		values[i] = temp2;
          		
          	}
          	
          	float cVal = finalVal - (finalVal*values[0]);
          	float remLoan = loan;
          	
          	int currentMonth = 1;
          	float prevValue = values[0];
          	
            //System.out.println(""+cVal+" "+remLoan); 
            
            int i = 1;
          	while(true)
          	{
          	    if(cVal > remLoan)
          	        break;
          	    
          	    if(i < values.length && currentMonth == years[i])
          	    {
          	        prevValue = values[i];
          	        i++;
          	    }
          	    
          	    cVal = cVal - (cVal*prevValue);
          	    remLoan -= installment;
          	    
          	    //System.out.println(""+cVal+" "+remLoan);
          	    currentMonth++;
          	    
          	}
          	
          	if((currentMonth-1) != 1)
          	{
          	    System.out.println(""+(currentMonth-1)+" months");
          	}
          	else
          	    System.out.println("1"+" month");
            
        }
      	
      	
     }
}



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

Re: 10114 - Loansome Car Buyer

Post by brianfry713 » Wed Nov 25, 2015 2:47 am

use class Main
Check input and AC output for thousands of problems on uDebug!

4rchitect
New poster
Posts: 2
Joined: Fri Mar 11, 2016 1:03 am

Re: 10114 - Loansome Car Buyer

Post by 4rchitect » Fri Mar 11, 2016 1:10 am

Wonder if someone could help me. I'm getting WA and I am not sure the reason.

Code: Select all

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int t, dr;
	double dp, al;
	
	while(scanf("%d %lf %lf %d", &t, &dp, &al, &dr) != EOF)
	{
		double vet[2][dr+1];
		int aTax = 0, aT = 0;
		double carValue = al+dp;
		dp = al/t;
		
		memset(vet,0,sizeof(vet));
		
		if(t < 0 )
			break;
		
		for(int i = 0; i < dr; i++)
			scanf("%lf %lf", &vet[0][i], &vet[1][i]);
			
		// Mês zero
		carValue *= (1-vet[1][0]);
		
		//cout << al << " " << carValue << " " << vet[1][0] << endl;
		
		if(carValue > al)
			cout << "0 month\n";
		else
		{
			for(int i = 1; i <= t; i++)
			{
				aT++;
				al -= dp;					
				
				if(vet[0][aTax+1] == i)
					aTax++;
					
				carValue *= (1-vet[1][aTax]);
				
				if(carValue > al)
					break;
				//cout << al << " " << carValue << " " << vet[1][aTax] << endl;
			}
			
			if(aT == 1)
				cout << "1 month\n";
			else
				cout << aT << " months\n";
		}
	}
	return 0;
}
EDIT: Had to change "0 month" to "0 months"

yingxuan
New poster
Posts: 2
Joined: Sat Jun 18, 2016 4:08 pm

Re: 10114 - Loansome Car Buyer

Post by yingxuan » Sun Jun 19, 2016 8:15 am

I have roamed the internet for many test cases for this problem and I passed all of them but I got a WA. Why is this?
As follows in my code.

Code: Select all

#include <bits/stdc++.h>
using namespace std;

int main(){
    int months;
    int ans;
    while (cin >> months){
        if (months < 0){
            int a,b,c;
            cin >> a >> b >> c;
            break;
        }
        double down, loan;
        int intmonth;
        cin >> down >> loan >> intmonth;
        vector <pair <int,double> > rates;
        for (int i = 0 ; i < intmonth ; i++){
            int x;
            double y;
            cin >> x >> y;
            rates.push_back(make_pair(x,y));
        }
        vector <double> ratese(months+1);
        int count = 0;
        for (int i = 0 ; i < months+1; i++){
            if (i == rates[count].first){
                ratese[i] = rates[count].second;
            }
            else if (i > rates[count].first){
                if (i == rates[count+1].first){
                    count++;
                    ratese[i] = rates[count].second;
                }
                else if (i < rates[count+1].first){
                    ratese[i] = rates[count].second;
                }
            }
        }
        double worth = down + loan;
        double owe = loan;
        worth *= 1-ratese[0];
        double each = owe/months;
        if (owe < worth){
            ans = 0;
        }
        else{
            for (int i = 1 ; i <= months ; i++){
                owe -= each;
                worth *= 1-ratese[i];
                //cout << owe << " " <<worth <<endl;
                if (owe < worth){
                    ans = i;
                    break;
                }

            }
        }
        if (ans == 1){
            cout << "1 month" <<endl;
        }
        else if (ans == 0){
            cout << "0 months" <<endl;
        }
        else{
            cout << ans << " months"<<endl;
        }
    }
    
    

}
Please helppp!!! Thank you :)

yingxuan
New poster
Posts: 2
Joined: Sat Jun 18, 2016 4:08 pm

Re: 10114 - Loansome Car Buyer

Post by yingxuan » Tue Jun 21, 2016 5:51 am

Solved it. Indexing problem. Thank you :)

lgtout
New poster
Posts: 2
Joined: Fri Jan 06, 2017 6:05 am

Re: 10114 - Loansome Car Buyer

Post by lgtout » Fri Jan 06, 2017 6:15 am

Hi,

I'd really appreciate it if someone could explain to me why the correct answer to this input is "1 month":

Code: Select all

12 500 9999.99 2
0 .05
2 .1 
-99
Thanks much.

lgtout
New poster
Posts: 2
Joined: Fri Jan 06, 2017 6:05 am

Re: 10114 - Loansome Car Buyer

Post by lgtout » Sat Jan 07, 2017 4:04 am

lgtout wrote:Hi,

I'd really appreciate it if someone could explain to me why the correct answer to this input is "1 month":

Code: Select all

12 500 9999.99 2
0 .05
2 .1 
-99
Thanks much.
Never mind. I realized the reason why my solution was wrong was that, from the first example given in the problem description, I assumed that the down payment was the same as the monthly payment. But that's only true in some cases. Otherwise, the monthly payment needs to be computed based on the loan amount and loan duration.

steventhai
New poster
Posts: 5
Joined: Thu May 11, 2017 5:06 pm

Re: 10114 - Loansome Car Buyer

Post by steventhai » Thu May 11, 2017 7:57 pm

Hello all,

I tried brianfry713 inputs, and it passed, but my program kept getting RTE. Please help.

Link: http://ideone.com/NiZDK6

Thanks.

steventhai
New poster
Posts: 5
Joined: Thu May 11, 2017 5:06 pm

Re: 10114 - Loansome Car Buyer

Post by steventhai » Thu May 11, 2017 8:58 pm

Nvm. I found the issue. I should always use Main as the class name.

Post Reply

Return to “Volume 101 (10100-10199)”