The learning task of the previous days was to use the VS tool code to implement the console to display calendars for any year and month. Because of the relationship between the year and the month, it is easy to take into account the situation of the year, so the function needs in the judgement of the run year and the relative number of months of judgement using multiple for loop and if judgment can be easily achieved. But one of the hardest and most important things about this task is that the calendar of the month will take the week of the month and show a certain amount of whitespace:
The realization of this function, need to find the law of every month blank. In the absence of a hint, Satsuma observed a number of months of the law, to get such a relationship: set a January 1, 1900 ago for 1 blank, then the end of the month has 5*7-1-the number of days is 35-1-31=3 a blank, then the beginning of February is 7-3 = 4 blank, and so on to a relationship, Set a blank book for the month as I, the month-end blank number is 35-i-month of the day, and the second month I next month =7-on the end of the month, I =i last month + last month 28, and when i=7, in fact there is no blank that is if (i==7) {i=0}; You can get an algorithm code that determines how many blanks are available before the calendar month:
for(intI=1, j=1900, day,month=1; j<=year&&month<= A; j++,month++){if(i==7) {i=0;Continue;}Else if(month==2){if(j%4==0&&j% -!=0|| j% -==0) { Day= in;}Else{ Day= -;}Else if(month<=7&&month%2!=0|| Month>7&&month%2==0) { Day= to;}Else{ Day= -;} I=i+day- in}}
The basic idea is to use the previously obtained relationship, through the loop, in order to calculate from January 1, 1900 to enter the year after the month of the date to exit the loop output I value. Before learning function recursion, it is conceivable that this algorithm, which does not use the analogy of loops, is tedious and amateur, and the correctness of the code is not guaranteed. After that, the teacher gave a new idea of the algorithm, that is, regardless of the year, the number of months before the calendar is the month of the month 1th (of course, if the calendar is calculated from Monday, that is, 1th week of the month-1), so as long as the user entered the year and month, January 1 is the day of the week, You can easily know the number of blank numbers in the calendar of the month, thus turning the question into a search week. To know the week of January 1 of a certain year, can be in a January 1, 1900 for the Monday conditions, respectively, from 1900 to the user entered the year after the number of days and the user entered the year of the year from January 1 to the user entered the month after the number of days, to obtain the total number of days, Find out 7 to know the month of January 1 is the day of the week. As for the run common year and the size of the month judgment, self-evident. That
list<string> Day =Newlist<string>(); intDayspassedofyears =0; for(inti =1900; i < year; i++) { ifI -==0|| I4==0&& I% -!=0) {Dayspassedofyears+=366; } Else{dayspassedofyears+=365; } } intDayspassedofmonth =0; for(inti =1; I < month; i++) { if(i = =2) { if(Year% -==0|| Year%4==0&& Year% -!=0) {Dayspassedofmonth+= in; } Else{dayspassedofmonth+= -; } } Else if(I <7&& I%2!=0|| i >7&& I%2==0) {Dayspassedofmonth+= to; } Else{dayspassedofmonth+= -; } } intdayspassed = Dayspassedofmonth +Dayspassedofyears; intDayOfWeek = dayspassed%7+1; intBlanks =DayOfWeek; if(Blanks = =7) {Blanks=0; } for(inti =0; I < blanks; i++) {Day. ADD (" "); } #endregion #regionGet all days of the monthintJ; if(month==2) { if(Year% -==0|| Year%4==0&& Year% -!=0) {J= in; } Else{J= -; } } Else if(Month <=7&& Month%2!=0|| Month >7&& Month%2==0&& Month! =2) {J= to; } Else{J= -; } for(inti =1; I <= J; i++) {Day. ADD (i.ToString ()); }
The algorithm is clear and easy to analyze the problem. In fact, after the subsequent study of recursion, for the previous algorithm, you can use recursive way to repeatedly use the function. However, it is not clear whether the calculation speed and memory consumption comparison. In order to solve the problem of the different ideas brought about by different methods of thinking, the beginners should sink their hearts, carefully analyze the problem, try to think of different algorithms, to take advantage of the people. On the one hand, it is also a mature program developers have to consider the problem
VS Tool-making console calendar inspired by algorithms