fork(6) download
  1. #include <stdio.h>
  2. int get_day(int yyyy, int mm);
  3. int get_day_of_month(int yyyy, int mm);
  4. int is_leaf_year(int yyyy);
  5. void print_cal(int start_day, int day_num);
  6. int main(void)
  7. {
  8. int yyyy, mm; scanf("%d%d",&yyyy, &mm);
  9. int start_day = get_day(yyyy, mm);
  10. int day_num = get_day_of_month(yyyy, mm);
  11. print_cal(start_day, day_num);
  12. }
  13. void print_cal(int start_day, int day_num)
  14. {
  15. printf(" Sun Mon Tue Wed Thu Fri Sat\n");
  16. for(int i = 0; i < start_day; i++)
  17. printf(" ");
  18. for(int day = 1, ke = start_day; day <= day_num; day++, ke++)
  19. {
  20. printf("%4d",day);
  21. if(ke%7 == 6)
  22. printf("\n");
  23. }
  24. }
  25. int get_day(int yyyy, int mm)
  26. {
  27. int past = 0;
  28. for(int y=1; y<yyyy; y++)
  29. past = past + 365 + is_leaf_year(y);
  30. for(int m=1; m<mm; m++)
  31. past = past + get_day_of_month(yyyy, m);
  32. return (1 + past) % 7;
  33. }
  34. int get_day_of_month(int yyyy, int mm)
  35. {
  36. int day_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  37. day_of_month[2] += is_leaf_year(yyyy);
  38. return day_of_month[mm];
  39. }
  40. int is_leaf_year(int yyyy)
  41. {
  42. if(yyyy % 400 == 0)
  43. return 1;
  44. if((yyyy % 100 != 0) && (yyyy % 4 == 0))
  45. return 1;
  46. return 0;
  47. }
Success #stdin #stdout 0s 4984KB
stdin
2020 2
stdout
 Sun Mon Tue Wed Thu Fri Sat
                           1
   2   3   4   5   6   7   8
   9  10  11  12  13  14  15
  16  17  18  19  20  21  22
  23  24  25  26  27  28  29