#UVa:10114-Loansome Car Buyer

灆洢 2019-03-29 09:10:05

車子的價值從 loan + downPayment 開始算起,每個月依照所給予的 depreciations 去降低它的價值;而你的債務從 loan 開始,每個月減少 loan / month (month 為你總共要繳的月份數量)。接著每個月去計算看看到哪個月的時候車子的價值大過於你的債務,則那個月即是答案。

P.S. 0 個月是 0 months 不是 0 month,這個錯誤害我找了很久……

C++(0.000)

/*******************************************************/
/* UVa 10114 Loansome Car Buyer                        */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2019/03/29                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

struct Depreciation{
  int month;
  double percentage;
};

int main(){
  int month, recordCount;
  double downPayment, loan;
  while(scanf("%d%lf%lf%d", &month, &downPayment, &loan, &recordCount) != EOF &&
    month >= 0 && downPayment >= 0 && loan >= 0 && recordCount >= 0){
    vector<Depreciation> depreciations(recordCount);
    for (int i = 0; i < recordCount; ++i){
      scanf("%d%lf", &(depreciations[i].month), &(depreciations[i].percentage));
    }

    int currentDepreciationIndex = 0;
    double value = loan + downPayment;
    double monthPay = loan / month;
    int currentMonth;
    for (currentMonth = 0; currentMonth < month; ++currentMonth){
      if(currentDepreciationIndex < recordCount - 1 &&
        depreciations[currentDepreciationIndex + 1].month <= currentMonth){
        ++currentDepreciationIndex;
      }

      value *= (1 - depreciations[currentDepreciationIndex].percentage);
      if(value >= loan){
        break;
      }

      loan -= monthPay;
    }

    printf("%d month%s\n", currentMonth, (currentMonth == 1)? "" : "s");
  }

  return 0;
}

在〈“#UVa:10114-Loansome Car Buyer”〉中有 1 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料