swust oj problem solving #509 bedroom sweeping (nanny teaching)

Posted by spooke2k on Mon, 22 Nov 2021 22:45:08 +0100

catalogue

subject

Train of thought analysis

code implementation

subject

Train of thought analysis

According to the idea of this question, we might as well make a calendar for 2007

Then, according to the requirements of the topic, discharge the floor sweepers

MonTueWedThuFriSatSun
September1(B)2(X)
3(ALL)4(H)5(P)6(B)7(X)8(H)9(P)
10(ALL)11(B)12(X)13(H)14(P)15(B)16(X)
17(ALL)18(H)19(P)20(B)21(X)22(H)23(P)
24(ALL)25(B)26(X)27(H)28(P)29(B)30(X)
October1(ALL)2(H)3(P)4(B)5(X)6(H)7(P)
8(ALL)9(B)10(X)11(H)12(P)1314
15(ALL)161718192021
222324252627

28

293031
November1234

According to the table, I must have found out the law, that is, a cycle of 14 days.   Therefore, happily start playing table life

//c + + code
string name[14]={"B","X","ALL","H","P","B","X","H","P","ALL","B","X","H","P"};//Typed by 
//c language code
char name[][14]={"B","X","ALL","H","P","B","X","H","P","ALL","B","X","H","P"};//Typed by 

  Since it is a cycle of 14 days, we can calculate the total number of days, and then mod14.

However, the dates of each month are different, so they start a happy watch life again

int Month[14]={0,31,28,31,30,31,30,31,31,30,31,30,31,0};//Enter the month by typing the table 

Suppose the target date is October 20, 2007, which is 49 days by oral calculation, and then we can easily come up with a violent solution.

int year=2007,month=10,day=20;
int sum=0;
for(int i=9;i<=month;i++){
      sum+=Month[i];
}
sum+=365*(year-2007);
sum+=day-1;

But what about 2008? What about 2009? (because the initial value of i is 9, if it's for April, this code won't work.) then go to if again   else special judgment, this seems a little complicated.  

Therefore, we might as well do a process to calculate the number of days from the first day of each month to the end of the year.

int Month[14]={0,31,28,31,30,31,30,31,31,30,31,30,31,0};
int sum=0,month=10,day=20;
//The suffix is summed so that Month[i] represents the number of days to the end of the year
	for(int i=12;i>=1;i--){
		Month[i]+=Month[i+1];
	}
//The difference from the number of days on September 1 can be expressed in this way
    sum=Month[9]-Month[month]+day-1;

You must be a little confused when you see here.

Due to the suffix and processing, in fact, the current Month[9]==30+31+30+31=122, that is, the number of days from September 1 to December 31 at the end of the year. Since the assumed month=10, Month[10]=31+30+31=92, that is, the number of days from October 1 to the end of the year. The subtraction of the two formulas is exactly 30 days, that is, the number of days in September. Then we add the number of days of day - 1 (minus September 1) to make the total number of days 49.

Then you must think I'm so superfluous. Let's take another example, February 10, 2008.

Or did you know by oral arithmetic that it was 162 days

int Month[14]={0,31,28,31,30,31,30,31,31,30,31,30,31,0};
int sum=0,year=2008,month=2,day=10;
//The suffix is summed so that Month[i] represents the number of days to the end of the year
	for(int i=12;i>=1;i--){
		Month[i]+=Month[i+1];
	}
//The difference from the number of days on September 1 can be expressed in this way
    sum=Month[9]-Month[month]+day-1;
    sum+=365*(year-2007);

Due to the suffix and processing, the current Month[2] is 334, that is, the number of days from February 1 to December 31.

Bring in the formula to calculate sum=365+122-334+10-1=162. The calculated results are in perfect agreement with the speculation.

To sum up, the summation code is:

sum=365*(year-2007)-Month[month]+Month[9]+day-1;

But don't think it's over. There's still a hole in the problem, that is, 2008 is a leap year!!! There are 29 days in February.

Through the previous summation, it is known that the period from September 1, 2007 to February 28, 2008 is exactly 180 days.

Therefore, when sum > 180, let sum + +. Careful students have found that there will be no 181?

Yes, 181 is February 29, so let's list it separately.

if(sum>180) sum++;
if(year==2008&&month==2&&day==29) sum=181;

Well, it took so long to sum up, and then it's output.

Because of the previous rule, we know that there is a cycle in 14 days, so we only need sum%14, that is, output name[sum%14].  

Finally, combine the above bulk codes.

code implementation

c language

#include<bits/stdc++.h>
int Month[14]={0,31,28,31,30,31,30,31,31,30,31,30,31,0};//Enter the month by typing the table 
char name[][14]={"B","X","ALL","H","P","B","X","H","P","ALL","B","X","H","P"};//Typed by 
int main()
{
    //Enter month, year and day 
    int year,month,day,sum;
	scanf("%d %d %d",&year,&month,&day);
	//Suffix and 
	for(int i=12;i>=1;i--){
		Month[i]+=Month[i+1];
	}
	//Calculate total days
	sum=365*(year-2007)-Month[month]+Month[9]+day-1;
	if(sum>180) sum++;
	if(year==2008&&month==2&&day==29) sum=181;
    //output
	printf("%s\n",name[sum%14]);
	return 0;
}

C++

#include<bits/stdc++.h>
using namespace std;
int Month[14]={0,31,28,31,30,31,30,31,31,30,31,30,31,0};//Enter the month by typing the table 
string name[14]={"B","X","ALL","H","P","B","X","H","P","ALL","B","X","H","P"};//Typed by 
int main()
{
    //Enter month, year and day 
    int year,month,day,sum;
	cin>>year>>month>>day;
	//Suffix and 
	for(int i=12;i>=1;i--){
		Month[i]+=Month[i+1];
	}
	//Calculate total days
	sum=365*(year-2007)-Month[month]+Month[9]+day-1;
	if(sum>180) sum++;
	if(year==2008&&month==2&&day==29) sum=181;
	//output 
	cout<<name[sum%14]<<endl;
	return 0;
}

 

Topics: C C++