using namespace std; #include <iostream> #include <cstring> #include <sstream> int sum[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; bool judge(int y){ return (y%4 == 0&nbs***bsp;y%400 == 0) and y%100 != 0; } template<class out_type, class in_value> out_type convert(const in_value & t) { stringstream stream; stream << t; out_type result; stream >> result; return result; } int Div(int y, int x){ int ans(0); if(abs(y-x) >= 1){ int low = min(y, x), high = max(y, x); while(low < high){ if(judge(low))ans += 366; else ans += 365; ++low; } } return ans; } int count(int y,int m, int d){ int ans(0); if(judge(y) and m>2)ans++; ans += sum[m-1] + d; return ans; } void change(int &y, int &m, int &d,string n){ y = convert<int>(n.substr(0, 4)); m = convert<int>(n.substr(4, 2)); d = convert<int>(n.substr(6, 2)); } int main(){ string n1, n2; while(cin>>n1>>n2){ int y1, y2, m1, m2, d1, d2, c1, c2; change(y1, m1, d1, n1); change(y2, m2, d2, n2); c1 = count(y1, m1, d1); c2 = count(y2, m2, d2); cout<<Div(y1, y2) + c2 - c1 + 1; } }
import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ LocalDate date1 = getDate(scanner.next()); LocalDate date2 = getDate(scanner.next()); System.out.println(date1.until(date2, ChronoUnit.DAYS)+1); } } static LocalDate getDate(String s){ String year = s.substring(0, 4); String month = s.substring(4, 6); String day = s.substring(6); return LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day)); } }
#include<stdio.h> #define isyear(x) x%10!=0&&x%4==0||x%400==0?1:0 int dom[13][2] { 0,0, 31,31, 28,29, 31,31, 30,30, 31,31, 30,30, 31,31, 31,31, 30,30, 31,31, 30,30, 31,31, }; typedef struct data { int year; int month; int day; void addday() { day++; if(day>dom[month][isyear(year)]) { day=1; month++; if(month>12) { month=1; year++; } } } }data; int buf[5000][13][31]={0}; int ABS(int a){return a<0?-a:a;} int main() { int cnt=0; data tem; tem.year=0; tem.month=1; tem.day=1; while(tem.year!=5000) { tem.addday(); cnt++; buf[tem.year][tem.month][tem.day]=cnt; } int y1,m1,d1; int y2,m2,d2; while(scanf("%4d%2d%2d",&y1,&m1,&d1)!=EOF) { scanf("%4d%2d%2d",&y2,&m2,&d2); printf("%d",ABS(buf[y1][m1][d1]-buf[y2][m2][d2])+1); } return 0; }
思路:
将小的日期记为A,大的日期记为B
计算 A年的 1.1 (A11) 到B年1.1 (B11)的天数 A11toB11 (记得计算闰年)
计算 A到本年的日期 Ato11
计算 B到本年的日期 Bto11
日期差值为
A11toB11 + Bto11 - Ato11
代码如下:
#include
#include
#include
using namespace std;
int beginDayOfMonth[13][2] = {
0, 0,
0, 0,
31, 31,
59, 60,
90, 91,
120, 121,
151, 152,
181, 182,
212, 213,
243, 244,
273, 274,
304, 305,
334, 335
};
int isRunYear(int year)
{
return year%100 != 0 && year%4 == 0 || year%400 == 0 ? 1:0;
}
int dayto11(int year, int month, int day)
{
int isRun = isRunYear(year);
if(isRun)
{
return day + beginDayOfMonth[month][1];
}
else return day + beginDayOfMonth[month][0];
// return day + beginDayOfMonth[month][isRun]; 但这里需要保证 isRun 只取 01
}
int main()
{
int AtoA11, BtoB11, A11toB11;
int y1, m1, d1;
int y2, m2, d2;
int disDay;
while(scanf("%4d%2d%2d", &y1, &m1, &d1) != EOF)
{
scanf("%4d%2d%2d", &y2, &m2, &d2);
if(y1 > y2 || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2))
{
int tmp = y1;
y1 = y2;
y2 = tmp;
tmp = m1;
m1 = m2;
m2 = tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}
AtoA11 = dayto11(y1, m1, d1);
BtoB11 = dayto11(y2, m2, d2);
// cout << AtoA11 << endl;
// cout << BtoB11 << endl;
A11toB11 = 0;
for(int i = y1; i < y2; i++)
{
if(isRunYear(i)) A11toB11 += 366;
else A11toB11 += 365;
}
disDay = A11toB11 + BtoB11 - AtoA11 + 1; // 这里是 把 最后一天的也给算上
printf("%d\n", disDay);
}
return 0;
}
思路二
主要参考“看不见的风景201803281904”。
这里我对计算距离0年1月1日的方法进行了修改,
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int mon[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int sumRun1(int y)
{
// 计算[0, y]之间 闰年 的次数
// 若 y在 0000 年之前, 则为负数,记录了[y, -1]闰年的次数(的相反数)
return 1 + floor(double(y-1)/4+1e-4) - floor(double(y-1)/100+1e-4) + floor(double(y-1)/400+1e-4);
// y-1是因为如果 第y年是闰年,那么会对 00000101至 y+1年1月1日 的天数造成影响
// +1 是为了 将0000年记为闰年
// floor(double(y-1)/4+1e-4) 是为了 计算在公元前的闰年
// + 1e-4 是为了避免浮点误差
/*
其实这段代码的效果就是为了对应
年数
-5 -4 -3 -2 -1 0 1 2 3 4 5 6
累计闰年个数
-1 -1 0 0 0 0 1 1 1 1 2 2
*/
}
int isRunYear(int year)
{
return year%100 != 0 && year%4 == 0 || year%400 == 0 ? 1:0;
}
int sumRun2(int y) // 和这个效果是一样的
{
int numOfRun = 0;
if(y < 0)
{
for(int i = y; i < 0; i++)
{
if(isRunYear(i)) numOfRun--;
}
}
else
{
for(int i = 0; i < y; i++)
{
if(isRunYear(i)) numOfRun++;
}
}
return numOfRun;
}
// 给出年月日,计算距离0000年 1 月 1 日的 天数
int calto01(int y, int m, int d)
{
return sumRun1(y) + y * 365 + mon[m-1] + d-1 + (((y%100 != 0 && y%4 == 0) || y%400 == 0) && m > 2);
}
int main()
{
int disto01[2], year, month, day;
int y0, m0, d0;
int y1, m1, d1;
while(scanf("%4d%2d%2d", &y0, &m0, &d0) != EOF)
{
scanf("%4d%2d%2d", &y1, &m1, &d1);
disto01[0] = calto01(y0, m0, d0);
disto01[1] = calto01(y1, m1, d1);
// cout << disto01[0] << " " << disto01[1] << endl;
printf("%d\n", abs(disto01[0] - disto01[1]) + 1);
}
return 0;
}
import java.io.BufferedInputStream; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class Main { public static void main(String... as) { Scanner sc = new Scanner(new BufferedInputStream(System.in)); LocalDate ld1 = LocalDate.parse(sc.next(), DateTimeFormatter.ofPattern("yyyyMMdd")); LocalDate ld2 = LocalDate.parse(sc.next(), DateTimeFormatter.ofPattern("yyyyMMdd")); System.out.println(ld1.until(ld2, ChronoUnit.DAYS) + 1); sc.close(); } }
//因为还不会格式输入,所以输入年月日的时候用了笨办法,希望大佬能帮忙改一下,谢谢!#include<iostream>
#define ISYEAP(x) ((x%4==0&&x%100!=0)||x%400==0) ?1:0
using namespace std;
int dayofmonth[13][2] = {
0,0,
31,31,28,29,31,31,30,30,31,31,30,30,
31,31,31,31,30,30,31,31,30,30,31,31
};
struct Date {
int day;
int month;
int year;
void nextday() {
day++;
if (day>dayofmonth[month][ISYEAP(year)])
{
day = 1;
month++;
if (month>12)
{
month = 1;
year++;
}//if
}//if
}//nextday
};//data
int buf[5001][12][32];
int abs(int x) {
return x<0 ? -x : x;
}
int main() {
Date tmp;
tmp.year = 0;
tmp.month = 1;
tmp.day = 1;
int num = 0;
while (tmp.year < 5001) {
buf[tmp.year][tmp.month][tmp.day] = num;
tmp.nextday();
num++;
}//while
int d1, m1, y1, temp1;
int d2, m2, y2, temp2;
while (cin >> temp1) {
d1 = temp1 % 100; temp1 = temp1 / 100;
m1 = temp1 % 100; y1 = temp1 / 100;
cin >> temp2;
d2 = temp2 % 100; temp2 = temp2 / 100;
m2 = temp2 % 100; y2 = temp2 / 100;
cout << abs(buf[y2][m2][d2] - buf[y1][m1][d1])+1<< endl;
}
return 0;
}
#include<stdio.h> #define leap(x) (x%400==0||(x%4==0&&x%100!=0)?1:0) #define Abs(x) ((x)>0?(x):-(x)) typedef struct date { int y, m, d; }Date; const int yd[2][13]={{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; int days(Date x) { int sum=0, i; int y=x.y, m=x.m, d=x.d; for(i=0; i<y; i++) { if(leap(i)) { sum+=366; } else { sum+=365; } } for(i=1; i<m; i++) { sum+=yd[leap(y)][i]; } sum+=d; return sum; } int main(void) { Date a, b; while(scanf("%4d%2d%2d", &a.y, &a.m, &a.d)!=EOF) { scanf("%4d%2d%2d", &b.y, &b.m, &b.d); printf("%d\n", Abs(days(a)-days(b))+1); } return 0; }
//计算两个日期距离0000年0月1日的天数再求差
#include <bits/stdc++.h>
using namespace std;
int mon[12]={0,31,59,90,120,151,181,212,243,273,304,334};//字典
int cal(int y,int m,int d)//给出年月日,计算距离0000年0月1日的天数和
{
return y*365+y/4-y/100+y/400+mon[m-1]+d-1+(y/100!=0&&y/4==0||y/400==0&&m>2);
}
int main()
{
int x[2],year,month,day;
for(int k=0;k<2;k++)//循环两次读入两个日期
{
scanf("%4d%2d%2d",&year,&month,&day);
x[k]=cal(year,month,day);
}
cout<<abs(x[0]-x[1])+1;
}
#include<bits/stdc++.h> using namespace std; const int month[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31 } }; bool leapyear(int y) { if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) return true; else return false; } int numofyear(int y) { if (leapyear(y)) return 366; else return 365; } int sumofdate(int y, int m, int d) { int sum = 0; for (int i = 0; i < y; i++) { sum += numofyear(i); } for (int i = 0; i < m; i++) { sum += month[leapyear(y)][i]; } sum += d; return sum; } int main() { int y1, m1, d1, y2, m2, d2; while (scanf("%04d%02d%02d", &y1, &m1, &d1) != EOF) { scanf("%04d%02d%02d", &y2, &m2, &d2); printf("%d\n", abs(sumofdate(y1, m1, d1)-sumofdate(y2, m2, d2))+1); } return 0; }
#include <iostream> #include <cstdio> using namespace std; int months_day[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; int isLoop(int year){ if((year%4==0 && year%100!=0) || year%400==0) return 1; else return 0; } int main(){ int year1,month1,day1; int year,month,day; while(scanf("%4d%2d%2d",&year,&month,&day)!=EOF){ int sum_days=0; scanf("%4d%2d%2d",&year1,&month1,&day1); if(year==year1 && month==month1) sum_days=abs(day-day1)+1; else if(year==year1){ if(month<month1){ for(int i=month;i<=month1;i++){ if(i==month) sum_days+=(months_day[month][isLoop(year)]-day+1); else if(i==month1) sum_days+=(day1); else sum_days+=months_day[i][isLoop(year)]; } }else{ for(int i=month1;i<=month;i++){ if(i==month1) sum_days+=(months_day[month1][isLoop(year)]-day1+1); else if(i==month) sum_days+=(day); else sum_days+=months_day[i][isLoop(year)]; } } }//elseif else{ if(year<year1){ for(int i=year;i<=year1;i++){ if(i==year){ for(int j=month;j<=12;j++){ if(j==month) sum_days+=(months_day[j][isLoop(i)]-day+1); else sum_days+=(months_day[j][isLoop(i)]); } } else if(i==year1){ for(int j=1;j<=month1;j++){ if(j==month1) sum_days+=(day1); else sum_days+=(months_day[j][isLoop(i)]); } } else{ if(isLoop(i)==1) sum_days+=366; else sum_days+=365; } } } else{ for(int i=year1;i<=year;i++){ if(i==year1){ for(int j=month1;j<=12;j++){ if(j==month1) sum_days+=(months_day[j][isLoop(i)]-day1+1); else sum_days+=(months_day[j][isLoop(i)]); } } else if(i==year){ for(int j=1;j<=month;j++){ if(j==month) sum_days+=(day); else sum_days+=(months_day[j][isLoop(i)]); } } else{ if(isLoop(i)==1) sum_days+=366; else sum_days+=365; } } } }//else cout<<sum_days<<endl; }//while return 0; }
#include<iostream> #include<cstdio> #include<string> #include<algorithm> using namespace std; int tab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}}; bool leapyear(int year) { return (year%4==0&&year%100!=0)||(year%400==0); } int numberofyear(int year) { if(leapyear(year)) return 366; else return 365; } int main() { char s1[9]; char s2[9]; while(cin>>s1>>s2) { int year1,year2,month1,month2,day1,day2; int num1=0; int num2=0; sscanf(s1,"%4d%2d%2d",&year1,&month1,&day1); sscanf(s2,"%4d%2d%2d",&year2,&month2,&day2); for(int i=0;i<year1;i++) { num1+=numberofyear(i); } for(int j=0;j<month1;j++) { num1+=tab[leapyear(year1)][j]; } num1+=day1; for(int i=0;i<year2;i++) { num2+=numberofyear(i); } for(int j=0;j<month2;j++) { num2+=tab[leapyear(year2)][j]; } num2+=day2; int num=abs(num2-num1)+1; cout<<num<<endl; } }
分别对年月日进行差值计算累加天数
#coding : utf-8
year = [365, 366]
month = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]]
def isRun(y):
if y%400==0 or (y%100!=0 and y%4==0):
return 1
return 0
def count(y1, m1, d1, y2, m2, d2):
cnt = 0
for y in xrange(y1, y2):
cnt+=year[isRun(y)]
y2_isRun = isRun(y2)
for m in xrange(m1, m2):
cnt+=month[y2_isRun][m-1]
return cnt+d2-d1+1
while True:
try:
date1 = raw_input().strip()
date2 = raw_input().strip()
y1 = int(date1[0:4])
y2 = int(date2[0:4])
m1 = int(date1[4:6])
m2 = int(date2[4:6])
d1 = int(date1[6:8])
d2 = int(date2[6:8])
if(y1>y2) or (y1==y2 and m1>m2) or (y1==y2 and m1==m2 and d1>d2):
y1 = int(date2[0:4])
y2 = int(date1[0:4])
m1 = int(date2[4:6])
m2 = int(date1[4:6])
d1 = int(date2[6:8])
d2 = int(date1[6:8])
print count(y1, m1, d1, y2, m2, d2)
except:
break
#include <cstdio> #include <iostream> using namespace std; class Date{ public: Date(int year=1,int month=1,int day=1){ _year=year; _month=month; _day=day; } Date(Date& d){ _year=d._year; _month=d._month; _day=d._day; } //计算每月天数 int GetMonthDay(int year, int month) { //用数组保存平年的月份天数 int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) return 29; return day[month]; } //重载前置++,先重载+= Date& operator+=(int day){ _day += day; while (_day > GetMonthDay(_year, _month)) { //日期减少,月份进位 _day -= GetMonthDay(_year, _month); ++_month; //月份上限继续进位 if (_month == 13) { ++_year; _month = 1; } } return *this; } //重载++ Date& operator++(){ *this+=1; return *this; } //重载== bool operator==(Date& d){ return _year==d._year&&_month==d._month&&_day==d._day; } //重载!= bool operator!=(Date& d){ return !(*this==d); } //重载> bool operator>(Date& d){ if(_year>d._year){ return true; }else if (_year==d._year &&_month>d._month) { return true; }else if (_year==d._year && _month==d._month && _day>d._day) { return true; }else{ return false; } } private: int _year; int _month; int _day; }; int main() { int Y1 = 0; int Y2 = 0; int M1 = 0; int M2 = 0; int D1 = 0; int D2 = 0; int n = 0; while (scanf("%4d%2d%2d", &Y1, &M1, &D1) !=EOF) { Date max(Y1, M1, D1); //获取第二个值 scanf("%4d%2d%2d", &Y2, &M2, &D2); Date min(Y2, M2, D2); //修正 if (min>max) { Date tmp(max); max = min; min = tmp; } while (min != max) { ++min; n++; } } int sum=n+1; cout<<sum<<endl; return 0; } // 64 位输出请用 printf("%lld")思路:重载运算符,定义一个最大日期和最小日期,可以修正。最小日期自增到最大日期,记录循环次数,由于多了一天,加上一天就是差值天数
#include <iostream> using namespace std; class Date { public: // 获取某日期的天数 int GetMonthDay(const Date& d) { // 用于记录每个月份的天数 static const int arr_day[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 如果是二月并且是闰年返回29天 if (d._month == 2 && ((d._year % 4 == 0) && (d._year % 100 != 0) || (d._year % 400 == 0))) { return 29; } return arr_day[d._month]; } // 初始化 void DateInit(int year, int month, int day) { _year = year; _month = month; _day = day; } // 重载运算符+= (日期+日期) Date& operator+=(int day) { _day += day; while (_day > GetMonthDay(*this)) { _day -= GetMonthDay(*this); ++_month; if (_month == 13) { ++_year; _month = 1; } } return *this; } // 比较两个日期的大小 // 如果年大就大,年小就小,年相等就比月,以此类推 bool operator<(const Date& d) { if (_year < d._year) { return true; } else if (_year == d._year) { if (_month < d._month) { return true; } else if (_month == d._month) { if (_day < d._day) { return true; } else { return false; } } else { return false; } } else { return false; } } // 重载运算符- (日期-日期) 返回天数 int operator-(const Date& d) { // flag 用于判断是正数还是负数 int flag = 1; // 先定义*this为大日期 Date max = *this; Date min = d; // 假设法,如果*this小于d就调整 if (*this < d) { min = *this; max = d; flag = -1; } // 用于记录天数 int day = 0; // 时间复杂度O(N) while (min < max) { min+=1; ++day; } return day * flag; } private: int _year; int _month; int _day; }; int main() { // 输入格式 int year1, month1, day1; int year2, month2, day2; scanf("%4d%2d%2d", &year1, &month1, &day1); //这里的格式 scanf("%4d%2d%2d", &year2, &month2, &day2); // 创建对象 Date d1; Date d2; d1.DateInit(year1, month1, day1); d2.DateInit(year2, month2, day2); cout << (d2 - d1) + 1 << endl; }