fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool isLeap(int y) {
  5. return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
  6. }
  7.  
  8. int daysInMonth(int m, int y) {
  9. int d[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  10. if (m == 2 && isLeap(y)) return 29;
  11. return d[m];
  12. }
  13.  
  14. int main() {
  15. int d, m, y, k;
  16. cin >> d >> m >> y >> k;
  17.  
  18. int numDays = daysInMonth(m, y);
  19.  
  20. int thu1 = ((k - (d - 1)) % 7 + 7) % 7;
  21. if (thu1 == 0) thu1 = 7;
  22.  
  23. int thuCuoi = (thu1 + numDays - 1) % 7;
  24. if (thuCuoi == 0) thuCuoi = 7;
  25.  
  26. int thuHaiDau = 0;
  27. for (int i = 1, t = thu1; i <= numDays; ++i, t = t % 7 + 1) {
  28. if (t == 2) {
  29. thuHaiDau = i;
  30. break;
  31. }
  32. }
  33.  
  34. int chuNhatCuoi = 0;
  35. for (int i = 1, t = thu1; i <= numDays; ++i, t = t % 7 + 1) {
  36. if (t == 8) chuNhatCuoi = i;
  37. }
  38.  
  39. cout << numDays << endl;
  40. cout << thuHaiDau << endl;
  41. cout << chuNhatCuoi << endl;
  42. cout << thu1 << endl;
  43. cout << thuCuoi << endl;
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5320KB
stdin
8 3 2024 6

stdout
31
4
0
6
1