这类题属于比较麻烦的一种题
一年中每个月有多少天(注意闰年的判断):

  1. void m2days(int year) {
  2. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
  3. days[2] = 29;
  4. else days[2] = 28;
  5. }
  6. int days[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

此外,一般第一年,第一个月,最后一年,最后一个月都是需要特殊考虑的情况

日期差值

  1. #include <time.h>
  2. #include <iostream>
  3. using namespace std;
  4. int days[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  5. void month2day(int year) {
  6. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
  7. days[2] = 29;
  8. else days[2] = 28;
  9. }
  10. int main() {
  11. #ifdef SUBMIT
  12. freopen("in.txt", "r", stdin);
  13. freopen("out.txt", "w", stdout);
  14. long _begin_time = clock();
  15. #endif
  16. int date1, date2;
  17. while (cin >> date1 >> date2) {
  18. int y1 = date1 / 10000, m1 = date1 / 100 % 100, d1 = date1 % 100;
  19. int y2 = date2 / 10000, m2 = date2 / 100 % 100, d2 = date2 % 100;
  20. int ans = 0;
  21. month2day(y1);
  22. if (y1 == y2) {
  23. if (m2 == m1) {
  24. ans += d2 - d1;
  25. }
  26. else {
  27. ans += days[m1] - d1;
  28. for (int i = m1 + 1; i < m2; i++) {
  29. ans += days[i];
  30. }
  31. ans += d2;
  32. }
  33. }
  34. else {
  35. ans += days[m1] - d1;
  36. for (int i = m1 + 1; i <= 12; i++) {
  37. ans += days[i];
  38. }
  39. for (int i = y1 + 1; i < y2; i++) {
  40. month2day(i);
  41. for (int j = 1; j <= 12; j++) {
  42. ans += days[j];
  43. }
  44. }
  45. month2day(y2);
  46. for (int i = 1; i < m2; i++) {
  47. ans += days[i];
  48. }
  49. ans += d2;
  50. }
  51. cout << ans + 1 << endl;
  52. }
  53. #ifdef SUBMIT
  54. long _end_time = clock();
  55. printf("\n\ntime = %ld ms", _end_time - _begin_time);
  56. #endif
  57. return 0;
  58. }

日期累加

  1. #include <time.h>
  2. #include <iostream>
  3. using namespace std;
  4. int days[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  5. void m2days(int y) {
  6. if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
  7. days[2] = 29;
  8. else days[2] = 28;
  9. }
  10. int main() {
  11. #ifdef SUBMIT
  12. freopen("in.txt", "r", stdin);
  13. freopen("out.txt", "w", stdout);
  14. long _begin_time = clock();
  15. #endif
  16. int m;
  17. cin >> m;
  18. while (m--) {
  19. int y, m, d, ds;
  20. cin >> y >> m >> d >> ds;
  21. int y1 = y, m1 = m, d1 = d;
  22. m2days(y);
  23. if (ds >= days[m] - d + 1) { // 注意+1,到下一个月
  24. ds -= days[m] - d + 1;
  25. d = 1;
  26. m = (m + 1) % 13; // 注意是%13
  27. if (m == 0) {
  28. m = 1;
  29. y++;
  30. }
  31. }
  32. else {
  33. d += ds;
  34. ds = 0;
  35. }
  36. while (ds > 0) {
  37. m2days(y);
  38. int nextm = (m + 1) % 13;
  39. if (ds >= days[m]) {
  40. d = 1;
  41. ds -= days[m];
  42. m = nextm;
  43. if (m == 0) {
  44. m = 1;
  45. y++;
  46. }
  47. }
  48. else {
  49. d = ds + 1; // 注意+1
  50. ds = 0;
  51. }
  52. }
  53. printf("%d-%02d-%02d\n", y, m, d);
  54. }
  55. #ifdef SUBMIT
  56. long _end_time = clock();
  57. printf("\n\ntime = %ld ms", _end_time - _begin_time);
  58. #endif
  59. return 0;
  60. }