Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
2611 孙浚轩 18岁生日 C++ Compile Error 0 MS 0 KB 1813 2024-01-15 22:22:51

Tests(0/0):


Code:

#include<iostream> using namespace std; struct Date { int year, month, day; }; bool isLeapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } int daysInMonth(int year, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return (isLeapYear(year) ? 29 : 28); default: return -1; // Invalid month } } int daysBetweenDates(const Date& start, const Date& end) { int days = 0; while (start.year < end.year || start.month < end.month || start.day < end.day) { int daysInCurrentMonth = daysInMonth(start.year, start.month); while (start.day < daysInCurrentMonth) { start.day++; days++; } if (start.month == 12 && start.day == daysInCurrentMonth) { start.year++; start.month = 1; start.day = 1; } else if (start.day == daysInCurrentMonth) { start.month++; start.day = 1; } } return days; } int main() { int t; cin >> t; while (t--) { Date birthDate, age18Date; char sep; cin >> birthDate.year >> sep >> birthDate.month >> sep >> birthDate.day; age18Date.year = birthDate.year + 18; age18Date.month = birthDate.month; age18Date.day = birthDate.day; if (age18Date.month == 2 && age18Date.day == 29 && !isLeapYear(age18Date.year)) { cout << -1 << endl; } else { int days = daysBetweenDates(birthDate, age18Date); cout << days << endl; } } return 0; }


Run Info:

Main.cc: In function 'int daysBetweenDates(const Date&, const Date&)':
Main.cc:31:22: error: increment of member 'Date::day' in read-only object
             start.day++;
                      ^
Main.cc:35:23: error: increment of member 'Date::year' in read-only object
             start.year++;
                       ^
Main.cc:36:25: error: assignment of member 'Date::month' in read-only object
             start.month = 1;
                         ^
Main.cc:37:23: error: assignment of member 'Date::day' in read-only object
             start.day = 1;
                       ^
Main.cc:39:24: error: increment of member 'Date::month' in read-only object
             start.month++;
                        ^
Main.cc:40:23: error: assignment of member 'Date::day' in read-only object
             start.day = 1;
                       ^