Daily Questions (97)
The 11th Lanqiao Cup C Language Group B Provincial Competition Exercise D: Running Exercise (10')
Ideas:
First set up an array of months to record the number of days in each month, and then consider the impact of leap year average years on the number of days in February.
Because the question asks how many kilometers, then the ordinary number of days 1 is equivalent to 1 kilometer, and the sum formula is:
The total number of days in 2000.1.1~2020.10.1 + the number of days in the beginning of the month but not Monday in 2000.1.1~2020.10.1 + the number of days in Monday in 2000.1.1~2020.10.1
The total number of days is easy to calculate, so how to consider the number of days at the beginning of the month but not Monday? We only need to set a judgment condition if ((the total number of days up to now + 6)% 7 != 1)
A friend will ask, why is it +6?
Because he said that 2000.1.1 is Saturday, it is equivalent to the original number shifted six units to the right from Sun. Is this better to understand?
The next step is to calculate the number of days on Monday. Let's draw a picture. The
orange line above is 2000.1. Let's just focus on 2000.1. How to divide it by 7? Of course it is minus 2 plus 6, so this is how (sum-2 + 6) / 7 comes from
Detailed C++ code:
#include<iostream>
using namespace std;
int sum = 0, s = 0;
int month[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
for(int i = 2000; i < 2020; i++)
{
if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
month[2]++; //闰年
}
for(int i = 1; i <= 12;i++)
{
if((sum + 6) % 7 != 1)
{
s++; //计算是月初且不是周一的日子
}
sum += month[i];
}
if(month[2] == 29)
{
month[2]--;
}
}
month[2]++;
//2020年的前9个月
for(int i = 1; i <= 9; i++)
{
if((sum + 6) % 7 != 1)
{
s++; //计算是月初且不是周一的日子
}
sum += month[i];
}
//加上2020.10.1日
sum += 1;
s++;
int z = sum + s + (sum - 2 + 6) / 7;
cout << z << endl;
return 0;
}
Operation result:
So the answer is 8879