fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int convert_weekday[] = { 8, 2, 3, 4, 5, 6, 7 };
  6. int calc_weekday(int dd, int mm, int yy) {
  7. //formula to get weekday number
  8. int rst =
  9. dd
  10. + ((153 * (mm + 12 * ((14 - mm) / 12) - 3) + 2) / 5)
  11. + (365 * (yy + 4800 - ((14 - mm) / 12)))
  12. + ((yy + 4800 - ((14 - mm) / 12)) / 4)
  13. - ((yy + 4800 - ((14 - mm) / 12)) / 100)
  14. + ((yy + 4800 - ((14 - mm) / 12)) / 400)
  15. - 32045;
  16.  
  17. return (rst + 1) % 7;
  18. }
  19.  
  20. bool isLeapYear(int year) {
  21. if (year % 4 != 0) return false;
  22. if (year % 100 != 0) return true;
  23. if (year % 400 != 0) return false;
  24. return true;
  25. }
  26.  
  27. int daysInMonth(int month, int year) {
  28. switch (month) {
  29. case 4:
  30. case 6:
  31. case 9:
  32. case 11: return 30;
  33. case 2: {
  34. if (isLeapYear(year)) return 29;
  35. return 28;
  36. }
  37. default: return 31;
  38. }
  39. }
  40.  
  41. int calc_special_day(int x, int y, int i, int j) {
  42. if (i < 1 || i > 12) {
  43. cout << -1;
  44. return 0;
  45. }
  46.  
  47. int day_index = 1;
  48.  
  49. int weekday_count = calc_weekday(1, i, j);
  50. while (convert_weekday[weekday_count] != y) {
  51. weekday_count = (weekday_count + 1) % 7;
  52. day_index++;
  53. }
  54.  
  55. int max_day_of_month = daysInMonth(i, j);
  56. int weekday_amount = (max_day_of_month - day_index) / 7 + 1;
  57.  
  58. if (x > 0 && x <= weekday_amount) return day_index + (x - 1) * 7;
  59. else return -1;
  60. }
  61.  
  62. int main() {
  63.  
  64. int x, y, i, j;
  65. cin >> x >> y >> i >> j;
  66. cout << calc_special_day(x, y, i, j);
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0s 5028KB
stdin
3
4
11
2016
stdout
16