如果输入1月1日(不考虑leap年),则计算给定年份中第13个星期一的数量。
我已经在StackOverflow上找到了this问答,但是它似乎太复杂了,无法解决我的作业问题。我只是一个数学专业的大一新生和C ++的初学者。因此,我想知道是否有更简单,更基本的方法来解决此问题?
好吧,在编写以下小代码后,我陷入了困境...
#include <bits/stdc++.h>
using namespace std;
int main()
{
enum Month = {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
enum week_day = {Mon, Tue, Wed, Thurs, Fri, Sat, Sun};
int jan1;
cin>>jan1
}
因为我无法确定计算星期一的最佳方式以及如何用代码编写的最佳方式。
谢谢。
答案 0 :(得分:1)
我不会为您做作业,但是下面的一些代码可以帮助您入门:
int monthdays[] = [0,31,28,31,30,31,30,31,31,30,31,30,31];
int day = 1;
int month = 1;
int week_day = 0; // monday is 0, tuesday is 1, ... you initialize this based on user input
int monday13count = 0;
while (month <= 12)
{
// your code goes here
// evaluate if this is "monday the 13th" => increment monday13count
// increment day and week_day.
// adjust for end of month condition
}