UVa10114 - Loansome Car Buyer

Kara Van and Lee Sabre are lonesome. A fewmonths ago they took out a loan to buy a new car, but nowthey're stuck at home on Saturday night without wheels and without money. You see, there was a wreck and the car was totaled. Their insurance paid $10,000, the current value of the car. The onlyproblem is that they owed the bank $15,000, and the bank wanted paymentimmediately, since there was no longer a car for collateral. In just a fewmoments, this unfortunate couple not only lost their car, but lostan additional $5,000 in cash too.

What Kara and Lee failed to account for was depreciation, the loss in valueas the car ages. Each month the buyer's loan payment reduces the amount owed on the car.However, each month, the car also depreciates as it gets older.Your task is to write a program thatcalculates the first time, measured in months, that a car buyer owes lessmoney than a car is worth. For this problem, depreciation is specified as a percentage of the previousmonth's value.

Input consists of information for several loans. Each loan consists ofone line containing the duration in months of the loan, the down payment,the amount of the loan, and the number of depreciation records that follow.All values are nonnegative, with loans being at most 100 monthslong and car values at most $75,000.Since depreciation is not constant, the varying rates are specified in a seriesof depreciation records.Each depreciation record consists of one line with a month number anddepreciation percentage, which is more than 0 and less than 1.These are in strictly increasing order by month, starting atmonth 0. Month 0 is the depreciation that applies immediately after driving the car off the lot and is always present in the data.All the other percentages are theamount of depreciation at the end of the corresponding month. Not all monthsmay be listed in the data. If a month is not listed, then the previous depreciation percentage applies. The end of the input is signalled by anegative loan duration - the other three values will be present butindeterminate.

For simplicity, we will assume a 0% interest loan, thus the car's initialvalue will be the loan amount plus the down payment. It is possiblefor a car's value and amount owed to be positive numbers less than $1.00.Do not round values to a whole number of cents ($7,347.635 should not be rounded to $7,347.64).

Consider the first example below of borrowing $15,000 for 30 months. As thebuyer drives off the lot, he still owes $15,000, but the car has dropped invalue by 10% to $13,950. After 4 months, the buyer has made4 payments, each of $500, and the car has further depreciated 3% in months 1and 2 and 0.2% in months 3 and 4. At this time, the car is worth$13,073.10528 and the borrower only owes $13,000.

For each loan, the output is the number of complete months before theborrower owes less than the car is worth. Note that English requiresplurals (5 months) on all values other than one (1 month).

Example input:

30 500.0 15000.0 3
0 .10
1 .03
3 .002
12 500.0 9999.99 2
0 .05
2 .1
60 2400.0 30000.0 3
0 .2
1 .05
12 .025
-99 0 17000 1

Example output:

4 months
1 month
49 months
#include <iostream>
#include <cstdio>

using namespace std;

const int N = 8192;

double m[N];
int month, number;
double payment, loan;

bool input();
void solve();

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	while (input()) {
		solve();
	}
	
	return 0;
}

bool input()
{
	cin >> month >> payment >> loan >> number;
	
	if (month < 0) return false;
	
	int j = 0;
	for (int i = 0; i < number; i++) {
		int k;
		double x;
		
		cin >> k >> x;
		
		for (; j < k; j++) {
			m[j] = m[j - 1];
		}
		m[j++] = x;
	}
	
	for (; j <= month; j++) {
		m[j] = m[j - 1];
	}
	
	return true;
}

void solve()
{
	double total = loan + payment;
	double p = loan / (double)month;
	total *= (1 - m[0]);
	
	int i;
	for (i = 0; i < month; i++) {
		if (loan < total) break;
		total *= (1 - m[i + 1]);
		loan -= p;
	}
	
	cout << i << " month" << (i != 1 ? "s" : "") << endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值