Topic 1096: Date Difference value java/c++

Source: Internet
Author: User

Title Description:

There are two dates, ask two days between dates, if two dates are contiguous we stipulate that the number of days between them is two days

Input:

There are more than one set of data, each set of data has two lines, representing two dates, in the form of YYYYMMDD

Output:

One row for each set of data, that is, the date difference value

Sample input:
2011041220110422
Sample output:
11


Java AC Code:

Import Java.util.Scanner;                public class Main {/** * @param args */public static void Main (string[] args) {                TODO auto-generated Method Stub int date1, Date2;                Scanner Inscanner = new Scanner (system.in);                        while (Inscanner.hasnext ()) {date1 = Inscanner.nextint ();                        Date2 = Inscanner.nextint ();                        int temp;                                if (Date1 > date2) {temp = Date1;                                Date1 = Date2;                        Date2 = temp;                        } int year1, month1, Day1, Year2, Month2, day2;                        Year1 = date1/10000;                        Month1 = (date1/100)% 100;                        Day1 = date1% 100;                        Year2 = date2/10000;                   Month2 = (date2/100)% 100;     Day2 = date2% 100;                System.out.println (diff (Year1, month1, Day1, Year2, Month2, day2));  }} public static Boolean Isleap (int.) {if (year% 4 = = 0 && Year% 100! = 0 | |                Year% = = 0) return true;                else {return false; }} public static int diff (int year1, int month1, int day1, int year2, int month2, I                NT day2) {int farAwayfromYuanDanofYear1 = Countdayssincenewyear (year1, Month1, day1);                                int farawayfromyuandanofyear2= countdayssincenewyear (year2, Month2, day2);                int count = 0;                if (year1 = = year2) {return Math.Abs (FARAWAYFROMYUANDANOFYEAR1-FARAWAYFROMYUANDANOFYEAR2) +1;                } else {for (int i = year1+1;i<year2;i++) {                if (Isleap (i)) count+=366;                                else {count+=365; }} if (Isleap (YEAR1)) return Count+farawayfro                        MYuanDanofYear2 + 367-farawayfromyuandanofyear1;                        else {return count+farawayfromyuandanofyear2 + 366-farawayfromyuandanofyear1; }}} P ublic static int countdayssincenewyear (int year, int month, Int. day) {int[] Leapmonth = {31, 29, 31, 30,                31, 30, 31, 31, 30, 31, 30, 31};                Int[] Commonmonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};                int Farawayfromyuadan = 0; if (Isleap (year)) {for (int i = 0; i < month;                        i++) {Farawayfromyuadan + = Leapmonth[i]; }} else {for (int i = 0; i < month; i++) {Faraw                        Ayfromyuadan + = Commonmonth[i];        }} return Farawayfromyuadan + day; }}/************************************************************** problem:1096 User:carvin Language:java Re sult:accepted time:90 Ms memory:15464 kb****************************************************************/

Java WA (why?) )

Import Java.util.Scanner;        public class main{public static void Main (string[] args) {int year1=0,year2=0;        int month1=0,month2 = 0;        int day1,day2;        int leapday1,leapday2;        int i;        String str1,str2;        Scanner cin=new Scanner (system.in);            while (Cin.hasnext ()) {year1=0;            year2=0;            leapday1=0;            leapday2=0;            Str1=cin.nextline ();            Str2=cin.nextline ();            Char Strarray1[]=str1.tochararray ();            Char Strarray2[]=str2.tochararray ();                for (i=0;i<4;i++) {year1=year1*10+strarray1[i]-' 0 ';            year2=year2*10+strarray2[i]-' 0 ';            } month1= (strarray1[4]-' 0 ') *10+strarray1[5]-' 0 ';            month2= (strarray2[4]-' 0 ') *10+strarray2[5]-' 0 ';            day1= (strarray1[6]-' 0 ') *10+strarray1[7]-' 0 ';            day2= (strarray2[6]-' 0 ') *10+strarray2[7]-' 0 '; for (i=0;i<year1| |   i<year2;i++)         {if (Leapyear (i) && (i<year1)) {leapday1++;                } if (Leapyear (i) && (i<=year2)) {leapday2++;            }} int Total_day1=year1*365+month (month1) +day1+leapday1;            int Total_day2=year2*365+month (month2) +day2+leapday2;        System.out.println ((total_day1>total_day2?total_day1-total_day2:total_day2-total_day1) +1);        }} public static int month (int y) {int a[]=new int[13];        int sum=0;        a[0]=0;        a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;        a[2]=28;        a[4]=a[6]=a[9]=a[11]=30;        for (int i=1;i<=y;i++) {sum+=a[i];    } return sum; public static Boolean leapyear (int y) {if (y% 400 = = 0 | |            (Y% = 0 && y% 4 = 0))        return true;    else return false; }}/******************problem:1096 User:carvin Language:java Result:wrong answer*** *************************************************************/

and C + + timeouts:

#include <iostream> #include <string> #include <cmath>using namespace std;bool leapyear (int y); int    month (int m); int main () {string str1;    String str2;    int year1=0,year2=0;    int month1,month2;    int day1,day2;    int i;        while (Getline (CIN,STR1)) {getline (CIN,STR2);        int leapday1=0,leapday2=0;            for (i=0;i<4;i++) {year1=year1*10+str1[i]-' 0 ';        year2=year2*10+str2[i]-' 0 ';        } month1= (str1[4]-' 0 ') *10+str1[5]-' 0 ';        month2= (str2[4]-' 0 ') *10+str2[5]-' 0 ';        day1= (str1[6]-' 0 ') *10+str1[7]-' 0 ';        day2= (str2[6]-' 0 ') *10+str2[7]-' 0 ';            for (i=0;i<year1;i++) {if (Leapyear (i)) {leapday1++;            }} for (i=0;i<year2;i++) {if (Leapyear (i)) {leapday2++;        }} int Total_day1=year1*365+month (month1) +day1+leapday1; int Total_day2=year2*365+month (MonTh2) +day2+leapday2;        cout<<total_day1<<endl;        cout<<total_day2<<endl;        Cout<<abs (total_day1-total_day2) +1<<endl;     cout<< (total_day1>total_day2?total_day1-total_day2:total_day2-total_day1) +1<<endl; } return 1;} BOOL Leapyear (int y) {if (y% 400 = = 0 | |        (Y% = 0 && y% 4 = 0))    return 1; else return 0;}    int month (int m) {int a[13];    int sum=0;    a[0]=0;    a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;    a[2]=28;    a[4]=a[6]=a[9]=a[11]=30;    for (int i=1;i<m;i++) {sum+=a[i]; } return sum;} /************************************************************** problem:1096 User:carvin language:c++ Resul T:time Limit exceed****************************************************************/



Topic 1096: Date Difference value java/c++

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.