如何算哪一天是星期几?
作者:
年岳.
,
2024-04-09 16:55:37
,
所有人可见
,
阅读 5
//自1582年10月15日过后的日期都可以根据下面的代码算出是星期几
#include <iostream>
using namespace std;
int main() {
int year, month, day;
cout << "请用数字输出年、月、日,并使用空格隔开:" << endl;
while (cin >> year >> month >> day) {
if (month < 3) {
year -= 1;
month += 12;
}
char week[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
int c = int(year / 100), y = year - 100 * c;
int w = int(c / 4) - 2 * c + y + int(y / 4) + (26 * (month + 1) / 10) +
day - 1;
w = (w % 7 + 7) % 7; //将可能出现的负数取余
cout << week[w] << endl;
}
return 0;
}