Question 1096: Date difference (computer research vitality test of Shanghai Jiao Tong University in 2009)

Source: Internet
Author: User
Tags date1
Description:

There are two dates. Calculate the number of days between two dates. If two dates are consecutive, we specify the number of days between them as two days.

Input:

There are multiple groups of data, each of which has two rows, representing two dates, in the form of yyyymmdd

Output:

Output a row of data in each group, that is, the date difference.

Sample input:
2011041220110422
Sample output:
11


The Code is as follows:
# Include "stdio. H "int dayofmonth [13] [2] = {, 31, 31, 31, 31, 30, 30, 31, 31, 31, 31, 30, 31, 31, 31, 31, 30, 31, 30, 30, 30, 31 }; typedef struct Date {int year; int month; int day;} date; int isrunnian (INT year) {// judge whether it is a leap year if (Year % 100! = 0 & year % 4 = 0) | (Year % 400 = 0) return 1; else return 0;} int compare (date D1, date D2) {If (d1.year> d2.year) return 1; else if (d1.year <d2.year) return 0; else {If (d1.month> d2.month) return 1; else if (d1.month <d2.month) return 0; else {If (d1.day> d2.day) return 1; else if (d1.day <d2.day) return 0; else return 2; // same date }}void nextday (date * Date) {// calculate the next day (* date ). day ++; If (* date ). day> dayofmonth [(* dat E ). month] [isrunnian (* date ). year)]) {(* date ). day = 1; (* date ). month ++; If (* date ). month> 12) {(* date ). month = 1; (* date ). year ++ }}int main (INT argc, char * argv []) {date date1, date2; while (scanf ("% 4D % 2D % 2D ", & date1.year, & date1.month, & date1.day )! = EOF) {scanf ("% 4D % 2D % 2D", & date2.year, & date2.month, & date2.day); // printf ("% d \ n", compare (date1, date2); // printf ("date1.year: % d date1.month: % d date1.day: % d \ n", date1.year, date1.month, date1.day); // printf ("date2.year: % d date2.month: % d date2.day: % d \ n ", date2.year, date2.month, date2.day); Date big_date, small_date; If (compare (date1, date2) = 1) {region = date1.year; big_date.month = date1.month; region = date1.day; region = date2.year; region = date2.month; region = date2.day;} else {region = date2.year; big_date.month = date2.month; region = date2.day; small_date.year = date1.year; small_date.month = date1.month; small_date.day = date1.day;} int COUNT = 1; // used to count while (compare (small_date, big_date) = 0 | compare (small_date, big_date) = 1) {// catch up with large dates with small dates, count ++; nextday (& small_date) ;} printf ("% d \ n ", count);} return 0 ;} /*************************************** * *********************** problem: 1096 User: lcyvino language: C result: accepted time: 10 MS memory: 912 KB ************************************** **************************/

 

Question 1096: Date difference (computer research vitality test of Shanghai Jiao Tong University in 2009)

Related Article

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.