[Blue Bridge Cup] Calculate the specified date as the day of the year

foreword

The Blue Bridge Cup National Software and Information Technology Professional Talent Competition is sponsored by the Talent Exchange Center of the Ministry of Industry and Information Technology, with more than 30,000 participants each year. As a leading national IT learning event in China, the Blue Bridge Cup Competition continues to strongly support comprehensive evaluation, scholarship evaluation, and postgraduate entrance examinations. It is an important competition for higher education teaching reform and innovative talent cultivation. So, from today I will analyze the major programming algorithm questions one by one from simple to difficult.
insert image description here

topic

What day of the year is m month d day. For example, January 1 of year y is the first day of that year, then July 7, 2000 is the first day of that year.

analyze

1. The problem is to calculate the number of a certain day in a year. Since February in a leap year is 29 days, we need to judge it is a leap year. 2. The
judgment rule of a leap year is once every four years. In other words, those that are divisible by 400, divisible by 4 and not divisible by 100 are all leap years.
3. Month rules 1, 3, 5, 7, 8, 10, and 12 are big months with 31 days; February is 28 in an ordinary year Days, leap year is 29 days; 4, 6, 9, 11 is a small month, there are 30 days

algorithm

1. Determine whether it is a leap year or a leap month according to the year. If it is a leap month, you need to set the current month to 29 days. 2.
Decrease and add the number of days from January to the target month. If the superimposed month is less than the target month, just increase the number of days in the cycle month; if cycle The month is the target month, and the number of days in the target date of the target month is directly increased.

difficulty

Difficulty is relatively simple, star*

combat

This question is a simple test question of the Blue Bridge Cup, and we will demonstrate it in JAVA language in this actual combat.

1. Create an algorithm

create algorithm

/**
 * 计算当年的第几天
 * @param year
 * @param month
 * @param day
 * @author senfel
 * @date 2023/4/11 10:53
 * @return void
 */
private static int computeDay(int year, int month, int day) {
    if (year == 0 || month == 0 || day == 0) {
        return 0;
    }
    //月份规则 1、3、5、7、8、10、12是大月,有31天;2月平年是28天,闰年是29天;4、6、9、11是小月,有30天
    //闰年规则 四年一润,百不润,四百又润
    //润年2月29天特殊处理,指定月份天数数组
    int mothArr[] ={31,28,31,30,31,30,31,31,30,31,30,31};
    //总天数
    int sum = 0;
    //从第一月依次增加天数至目标月份
    for(int i=0;i<month;i++){
        //当前月份的天数
        int days = mothArr[i];
        if(i == 1){
            //二月判断是否是闰年 索引从0开始故为2月
            if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
                //闰年二月为29天
                days = 29;
            }
        }
        if(i < month -1){
            //目标倒数第一月
            sum+=days;
        }else{
            //目标月份
            sum+=day;
        }
    }
    return sum;
}

2. Create test cases

test case

@SpringBootTest
class DemoApplicationTests {
    

    /**
     * 测试计算当前日期为当年第几天算法
     * @author senfel
     * @date 2023/4/11 11:29
     * @return void
     */
    @Test
    void computeDay() {
        //计算
        int sum = DayDemo.computeDay(2023, 12, 31);
        System.err.println("2023-12-31是当年第"+sum+"天");
        int sum2 = DayDemo.computeDay(2024, 12, 31);
        System.err.println("2024-12-31是当年第"+sum2+"天");
        int sum3 = DayDemo.computeDay(2000, 7, 7);
        System.err.println("2000-07-07是当年第"+sum3+"天");
    }

}

3. Test results

2023-12-31 is the 365th day of the year
2024-12-31 is the 366th day of the year
2000-07-07 is the 189th day of the year

Summarize

It is relatively simple to calculate the current date as the day of the year. We only need to understand the common sense of the leap month of the leap year and the number of days in December. The test questions are [Blue Bridge Cup] simple test questions. It can also be seen that many of the test questions of the Blue Bridge Cup are very close to our lives.

Guess you like

Origin blog.csdn.net/weixin_39970883/article/details/130080269
Recommended