The 11th Blue Bridge Cup-Running Exercise

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:

解题思路

  1. Use 0 ~ 6to represent 周一 至 周日;
  2. Used sumto represent 2000年1月1日to 某年某月某日the number of days elapsed;
  3. Since 2000年1月1日Saturday, then use (sum + 5) % 7can represent 某年某月某日the week;
  4. Because 2020年10月1日untreated, so in the end ans += 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

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/115093881