标签:boolean c++ 九度 online judge java
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
每组数据输出一行,即日期差值
20110412 20110422
11
Java AC 代码:
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 year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; else { return false; } } public static int diff(int year1, int month1, int day1, int year2, int month2, int 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+farAwayfromYuanDanofYear2 + 367 - farAwayfromYuanDanofYear1; else { return count+farAwayfromYuanDanofYear2 + 366 - farAwayfromYuanDanofYear1; } } } public 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++) { farAwayfromYuadan += commonMonth[i]; } } return farAwayfromYuadan + day; } } /************************************************************** Problem: 1096 User: Carvin Language: Java Result: Accepted Time:90 ms Memory:15464 kb ****************************************************************/
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 % 100 != 0 && y % 4 == 0)) return true; else return false; } } /************************************************************** Problem: 1096 User: Carvin Language: Java Result: Wrong Answer ****************************************************************/
#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 % 100 != 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++ Result: Time Limit Exceed ****************************************************************/
标签:boolean c++ 九度 online judge java
原文地址:http://blog.csdn.net/carvin_zh/article/details/45372087