# Simulation# Date Calculation

k847 - P1.租車費用 (Rent)

🔗 前往 ZeroJudge 原題

題目描述

題目要求計算從起始日期到結束日期之間經過的天數,並根據天數計算租車費用。租車費用為每天 100 元,但每 10 天可折抵 100 元。

解題思路

這題主要使用模擬的方式計算天數。程式碼中 m 陣列儲存了每個月的天數。程式碼從起始日期開始,每天增加日期,直到到達結束日期。在迴圈中,計算經過的天數 ct。最後,根據 ct 計算租車費用,並輸出結果。租車費用計算方式為 ct * 100 - ct / 10 * 100

複雜度分析

  • 時間複雜度: O(N),其中 N 是起始日期到結束日期之間的天數。
  • 空間複雜度: O(1),程式碼只使用了固定大小的陣列和變數。

程式碼

#include <iostream>
using namespace std;
int m[12]={31,28,31,30,31,30,31,31,30,31,30,31},x,y,xt,yt,ct=1;
int main(){
	cin.tie(0); ios::sync_with_stdio(0);
	cin >> x >> y >> xt >> yt;
	while(x!=xt||y!=yt){
		++y;
		++ct;
		if(y>m[x-1]){
			++x;
			y=1;
		}
	}
	cout << ct*100-ct/10*100;
}

Discussion