【PTA】Did the date succeed?

Please add image description
Personal business card:

blogger: Alcoholics ᝰ.
Personal profile: Indulge in wine, and strive for a future with a drink.
column: PTA exercises and analysis
introduce: Recorded the blogger's practice questions in pta

Please add image description

foreword

1 Introduction

"PTA programming experimental auxiliary teaching platform" is an auxiliary teaching platform for programming courses led by Zhejiang University, managed and operated by Hangzhou Baiteng Education Technology Co., Ltd., and jointly constructed by teachers in colleges and universities across the country. At present, 538 colleges and universities across the country have participated, and 3,152 teachers have jointly constructed 66,095 high-quality topics, and the number of registered students has reached 1.36 million; there are 14 fixed topic sets (covering C language, JAVA language, Python language, data structure, database system) and China University Computer Competition Question Bank) and 55 professional course topic sets (covering computer, electronics, literature, foreign language and Huawei certification). There are 10 types of questions including true and false questions, fill-in-the-blank questions, multiple-choice questions, multiple-choice questions, program fill-in-the-blank questions, function questions, programming questions, subjective questions, multi-file programming questions and SQL programming questions.

2. Advantages

  • Support 200,000 people online at the same time
  • Support multiple question types
  • Support a variety of judgment modes
  • Provide comprehensive guarantee for teaching quality
  • Instant question answering system

1. The topic

On September 9, 2021, Zhang San wanted to invite Xiaofang, a girl who had been secretly in love for a long time, to watch a movie. Xiaofang told Zhang San that she was not free on this day and could promise to watch it with him on a certain day in a certain year. Enter a date for an appointment, please write a program to determine whether Zhang San can finally fulfill his dating wish, if so, output yes, otherwise output no. Note that appointments cannot be timed back to September 9, 2021.

Input format:
Enter the year, month, and day in one line, separated by -.

Output format:
valid date output yes, invalid date output no

Input sample 1:

2022-7-7

Sample output 1:

yes

Input sample 2:

2022-2-29

Sample output 2:

no

Input sample 3:

2021-2-28

Sample output 3:

no

Code Length Limit 16 KB
Time Limit 400 ms
Memory Limit 64 MB

2. Code

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String[] times = str.split("-");
        int[] sum = new int[3];
        int i = 0;
        for (String time : times) {
    
    
            int num = Integer.parseInt(time);
            sum[i] = num;
            i++;
        }
        int year = sum[0];
        int month = sum[1];
        int day = sum[2];
        //2021年9.9之前
        if (year < 2021 ||( year == 2021 && month == 9 && day < 9) || (year == 2021 && month < 9)){
    
    
            System.out.println("no");
        } else {
    
    
            //二月,判断是否是闰年
            if (month == 2){
    
    
                if ((year % 4 ==0 && year % 100 != 0) || year % 400 == 0){
    
    
                    if (day > 29 || day < 1){
    
    
                        System.out.println("no");
                    }else {
    
    
                        System.out.println("yes");
                    }
                }else {
    
    
                    if (day > 28 || day < 1){
    
    
                        System.out.println("no");
                    }else {
    
    
                        System.out.println("yes");
                    }
                }
            }else {
    
    
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
    
    
                    if (day > 31 || day < 1){
    
    
                        System.out.println("no");
                    }else {
    
    
                        System.out.println("yes");
                    }
                }else {
    
    
                    if (day > 30 || day < 1){
    
    
                        System.out.println("no");
                    }else {
    
    
                        System.out.println("yes");
                    }
                }
            }
        }
    }
}

3. Notes

  1. If it is a multiple of 4, the year is generally a leap year; if it is not a multiple of 4, the year is generally a normal year. If the Gregorian calendar year is a whole hundred, it must be a multiple of 400 to be a leap year, otherwise it is a normal year.
  2. The leap year judgment programming is if (year%400==0||year%100!=0&&year%4 == 0).
  3. Condition:
    Divisible by 4 and not divisible by 100 Divisible by
    400

рекомендация

отblog.csdn.net/m0_65144570/article/details/127098718