儒略日转公历日期
#include
// 判断某年是否为闰年 bool isLeapYear(int year) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { return true; } return false; }
// 根据儒略日计算公历日期 void calculateDate(int julianDay, int& year, int& month, int& day) { // 公元前4713年1月1日为第0天 int days = julianDay - 0;
// 先计算年份
year = 4713 - 1; // 公元前1年
while (days >= 365) {
if (isLeapYear(year)) {
if (days >= 366) {
days -= 366;
year--;
} else {
break;
}
} else {
days -= 365;
year--;
}
}
// 再计算月份和日期
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
daysInMonth[1] = 29;
}
month = 0;
while (days >= daysInMonth[month]) {
days -= daysInMonth[month];
month++;
}
day = days + 1; // 日期从1开始计数
}
int main() { int Q; cin >> Q;
vector<int> julianDays(Q);
for (int i = 0; i < Q; i++) {
cin >> julianDays[i];
}
for (int i = 0; i < Q; i++) {
int year, month, day;
calculateDate(julianDays[i], year, month, day);
cout << day << ' ' << month << ' ';
if (year > 0) {
cout << year;
} else {
cout << -year << ' ' << 'BC';
}
cout << endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/qvlH 著作权归作者所有。请勿转载和采集!