特殊日曆計算 —— C++

有一種特殊的日曆法,它的一天和我們現在用的日曆法的一天是一樣長的。
它每天有10個小時,每個小時有100分鐘,每分鐘有100秒。
10天算一週,10周算一個月,10個月算一年。
現在要你編寫一個程序,將我們常用的日曆法的日期轉換成這種特殊的日曆表示法。
這種日曆法的時、分、秒是從0開始計數的。
日、月從1開始計數,年從0開始計數。
秒數爲整數。
假設 0:0:0 1.1.2000 等同於特殊日曆法的 0:0:0 1.1.0。

輸入
第一行是一個正整數 N ,表明下面有 N 組輸入。每組輸入有一行,格式如下:hour:minute:second day.month.year
表示常規的日期。日期總是合法的。2000 <= year <= 50000。

輸出
每組輸入要求輸出一行。格式如下:
mhour:mmin:msec mday.mmonth.myear
是輸入日期的特殊日曆表示方法。

樣例輸入

7
0:0:0 1.1.2000
10:10:10 1.3.2001
0:12:13 1.3.2400
23:59:59 31.12.2001
0:0:1 20.7.7478
0:20:20 21.7.7478
15:54:44 2.10.20749

樣例輸出

0:0:0 1.1.0
4:23:72 26.5.0
0:8:48 58.2.146
9:99:98 31.8.0
0:0:1 100.10.2000
0:14:12 1.1.2001
6:63:0 7.3.6848

代碼

#include<iostream>
#include<string>
using namespace std;
int years[] = { 366,365 };
int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int daysr[] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int isrun(int n) {
	if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0)
		return 0;
	return 1;
}
int main() {
	int n; cin >> n;
	while (n--) {
		string time, date;
		cin >> time >> date;
		
		int hour = stoi(time.substr(0, time.find(":")));
		time = time.substr(time.find(":") + 1);
		int minute = stoi(time.substr(0, time.find(":")));
		time = time.substr(time.find(":") + 1);
		int second = stoi(time);

		int day = stoi(date.substr(0, date.find(".")));
		date = date.substr(date.find(".") + 1);
		int month = stoi(date.substr(0, date.find(".")));
		date = date.substr(date.find(".") + 1);
		int year = stoi(date);
		
		int thedays = 0;
		for (int i = 2000; i < year; i++) {
			thedays += years[isrun(i)];
		}
		if (isrun(year) == 0)
			for (int i = 1; i < month; i++)
				thedays += daysr[i];
		else
			for (int i = 1; i < month; i++)
				thedays += days[i];
		thedays += (day-1);

		year = thedays / 1000;
		month = thedays % 1000 / 100 + 1;
		day = thedays % 1000 % 100 + 1;
		
		int thesecond = (hour * 3600 + minute * 60 + second)*125/108;
		hour = thesecond/ 10000;
		minute = thesecond % 10000 / 100;
		second = thesecond % 10000 % 100;

		cout << hour << ":" << minute << ":" << second << " " << day << "." << month << "." << year << endl;
	}
	system("pause");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章