Problem description
Xiaolan exercises every day.
-
Under normal circumstances, Xiaolan runs 1 kilometer every day.
-
If a day is Monday or the beginning of the month (1st), in order to motivate himself, Xiaolan will run 2 kilometers.
-
If it is also Monday or the beginning of the month, Xiaolan will also run 2 kilometers.
Xiaolan has been running for a long time, from Saturday, January 1, 2000 (inclusive) to Thursday, October 1, 2020 (inclusive).
How many kilometers did Xiaolan run during this period?
Answer submission
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it.
The result of this question is an integer. Only fill in this integer when submitting the answer, and fill in the extra content will not be scored.
Answer: 8879
answer:
解题思路
:
- Use
0 ~ 6
to represent周一 至 周日
; - Used
sum
to represent2000年1月1日
to某年某月某日
the number of days elapsed; - Since
2000年1月1日
Saturday, then use(sum + 5) % 7
can represent某年某月某日
the week; - Because
2020年10月1日
untreated, so in the endans += 2
;
#include <iostream>
using namespace std;
int days[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int year)
{
return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
int get_day(int year, int month)
{
if(month == 2) return 28 + is_leap(year);
return days[month];
}
int main()
{
int sum = 0, ans = 0;
for (int i = 2000; i <= 2019; i ++)
for (int j = 1; j <= 12; j ++)
for (int k = 1; k <= get_day(i, j); k ++)
{
int weekday = (sum + 5) % 7;
if(k == 1 || weekday == 0) ans += 2;
else ans ++;
sum ++;
}
for (int j = 1; j <= 9; j ++)
for (int k = 1; k <= get_day(2020, j); k ++)
{
int weekday = (sum + 5) % 7;
if(k == 1 || weekday == 0) ans += 2;
else ans ++;
sum ++;
}
cout << ans + 2 << endl;
return 0;
}
Lanqiao Cup C/C++ Group Provincial Competition Past Years Questions