Page 3 of 3

Re: 10114 - Loansome Car Buyer

Posted: Mon Nov 16, 2015 11:04 am
by samin
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");
            
        }
      	
      	
     }
}



Re: 10114 - Loansome Car Buyer

Posted: Wed Nov 25, 2015 2:47 am
by brianfry713
use class Main

Re: 10114 - Loansome Car Buyer

Posted: Fri Mar 11, 2016 1:10 am
by 4rchitect
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"

Re: 10114 - Loansome Car Buyer

Posted: Sun Jun 19, 2016 8:15 am
by yingxuan
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 :)

Re: 10114 - Loansome Car Buyer

Posted: Tue Jun 21, 2016 5:51 am
by yingxuan
Solved it. Indexing problem. Thank you :)

Re: 10114 - Loansome Car Buyer

Posted: Fri Jan 06, 2017 6:15 am
by lgtout
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.

Re: 10114 - Loansome Car Buyer

Posted: Sat Jan 07, 2017 4:04 am
by lgtout
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.

Re: 10114 - Loansome Car Buyer

Posted: Thu May 11, 2017 7:57 pm
by steventhai
Hello all,

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

Link: http://ideone.com/NiZDK6

Thanks.

Re: 10114 - Loansome Car Buyer

Posted: Thu May 11, 2017 8:58 pm
by steventhai
Nvm. I found the issue. I should always use Main as the class name.