24h購物| | PChome| 登入
2013-08-11 09:07:10| 人氣2,165| 回應0 | 上一篇 | 下一篇

[UVA][模擬] 10114 - Loansome Car Buyer

推薦 0 收藏 0 轉貼0 訂閱站台


Problem D: Loansome Car Buyer

Source file:loan.{c, cpp, java, pas}
Input file:loan.in
Output file:loan.out

Kara Van and Lee Sabre are lonesome. A few months ago they took out a loan to buy a new car, but now they'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 only problem is that they owed the bank $15,000, and the bank wanted payment immediately, since there was no longer a car for collateral. In just a few moments, this unfortunate couple not only lost their car, but lost an additional $5,000 in cash too.

What Kara and Lee failed to account for was depreciation, the loss in value as 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 that calculates the first time, measured in months, that a car buyer owes less money than a car is worth. For this problem, depreciation is specified as a percentage of the previous month's value.

Input consists of information for several loans. Each loan consists of one 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 months long and car values at most $75,000. Since depreciation is not constant, the varying rates are specified in a series of depreciation records. Each depreciation record consists of one line with a month number and depreciation percentage, which is more than 0 and less than 1. These are in strictly increasing order by month, starting at month 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 the amount of depreciation at the end of the corresponding month. Not all months may 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 a negative loan duration - the other three values will be present but indeterminate.

For simplicity, we will assume a 0% interest loan, thus the car's initial value will be the loan amount plus the down payment. It is possible for 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 the buyer drives off the lot, he still owes $15,000, but the car has dropped in value by 10% to $13,950. After 4 months, the buyer has made 4 payments, each of $500, and the car has further depreciated 3% in months 1 and 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 the borrower owes less than the car is worth. Note that English requires plurals (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 <stdio.h>
#include <algorithm>
using namespace std;

int main() {
    int n, m;
    int i, j;
    double paid, loan;
    while(scanf("%d %lf %lf %d", &n, &paid, &loan, &m) == 4) {
        if(n < 0)   break;
        double month, car, pp[105];
        int mm[105] = {};
        month = loan/n;
        for(i = 0; i < m; i++) {
            int x;
            double p;
            scanf("%d %lf", &x, &p);
            mm[x] = 1, pp[x] = p;
        }
        for(i = 1; i <= n; i++)
            if(mm[i] == 0)
                pp[i] = pp[i-1];
        car = (paid+loan)*(1-pp[0]);
        for(i = 1; i <= n; i++) {
            if(loan < car + 1e-8)  break;
            loan = loan - month;
            car = car*(1-pp[i]);
        }
        int ret = i-1;
        if(ret == 1)
            puts("1 month");
        else
            printf("%d months\n", ret);
    }
    return 0;
}

台長: Morris
人氣(2,165) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 10371 - Time Zones
此分類上一篇:[UVA][樹形dp] 12186 - Another Crisis

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文