OJ click to judge whether the date (year, month, and day) is valid

Source: Internet
Author: User

OJ click to judge whether the date (year, month, and day) is valid
Description

Compile the isValid_date function. The function declaration is as follows:
Int isValid_date (int year, int month, int day); // function declaration that determines whether the date (year, month, and day) is valid. if the date is valid, 1 is returned. if the date is invalid, 0 is returned.
Add the isValid_date function definition on the basis of the following programs so that the program can be correctly executed.
When submitting, you only need to submit the definition code of the isValid_date function.


# Include <iostream>
Using namespace std;


Int isValid_date (int year, int month, int day); // function declaration that determines whether the date (year, month, and day) is valid


Int main ()
{
Int y, m, d; // defines three variables to store the input year, month, and day.
Cin> y> m> d;
Cout <y <"-" <m <"-" <d;
If (isValid_date (y, m, d) = 1)
Cout <"valid" <endl;
Else
Cout <"not valid" <endl;
Return 0;
}

Input

Enter three positive integers, which represent the year, month, and day of a date.

Output

Whether the date is correct, for example, whether the month is correct, and whether the month date is correct

Sample Input
2002 13 40
Sample output
2002-13-40 not valid

 

 

The Code is as follows:

# Include <iostream> using namespace std; int isValid_date (int year, int month, int day); // function declaration that determines whether the date (year, month, and day) is valid int main () {int y, m, d; // defines three variables to store the input year, month, and day cin> y> m> d; cout <y <"-" <m <"-" <d; if (isValid_date (y, m, d) = 1) cout <"valid" <endl; else cout <"not valid" <endl; return 0;} int isValid_date (int year, int month, int day) {int days, I; for (I = 1; I <= month; I ++) {if (I = 1 | I = 3 | I = 5 | I = 7 | I = 8 | I = 1 0 | I = 12) days = 31; else if (I = 4 | I = 6 | I = 9 | I = 11) days = 30; else if (year % 4 = 0 & year % 100! = 0) | year % 400 = 0) days = 29; else days = 28 ;} if (year >=0 & month >=1 & month <= 12 & day >=1 & day <= days) return 1; else return 0 ;}


 

Running result:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.