【C++】计算日期到天数转换

时间:2025-04-05 22:06:47
#include <iostream> using namespace std; int main() { static int monthdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int year, month, day; while (cin >> year >> month >> day) { // 注意 while 处理多个 case // 使用while的话,可以不断接收输入;按ctrl + c可以结束循环。 int n = 0; //计算1-month-1月的天数 for (int i = 1; i < month; i++) { n += monthdays[i]; } //累加month那个月的天数 n += day; //如果是闰年,再加1; if (month > 2 &&((year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0))) { n += 1; } cout << n << endl; } return 0; } // 64 位输出请用 printf("%lld")