zoukankan      html  css  js  c++  java
  • CF w4d3 B. Calendar

    Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows:

    Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year.

    In this problem, you have been given two dates and your task is to calculate how many days are between them. Note, that leap years have unusual number of days in February.

    Look at the sample to understand what borders are included in the aswer.

    Input

    The first two lines contain two dates, each date is in the format yyyy:mm:dd (1900 ≤ yyyy ≤ 2038 and yyyy:mm:dd is a legal date).

    Output

    Print a single integer — the answer to the problem.

    Examples

    inputCopy
    1900:01:01
    2038:12:31
    outputCopy
    50768
    inputCopy
    1996:03:09
    1991:11:12
    outputCopy
    1579

    输入两个日期,算相差天数。
    用这种模拟很笨。

    #include<bits/stdc++.h>
    #include<stdio.h>
    using namespace std;
    int dy[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int ey[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; 
    struct data
    {
    	int y;
    	int m;
    	int d;
    }a,b;
    bool leap(int x)
    {
    	if(x%400==0||x%4==0&&x%100!=0)return true;//是闰年 
    	else return false;//不是闰年 
    }
    int deal_data(data a,data b)
    {
    	int sum=0;
    	if(a.y<=b.y)
    	{
    		if(a.y<b.y||a.y==b.y&&b.m>a.m||a.y==b.y&&a.m==b.m&&a.d<b.d)
    		{
    			//cout<<"-";
    			data tmp=b;
    			b=a;
    			a=tmp;	
    		}
    	}//保证a大b小
    	if(b.y==a.y)//年份相等 
    	{
    		if(b.m==a.m)sum+=a.d-b.d;
    		else
    		{
    			if(leap(b.y))sum+=ey[b.m]-b.d;
    			else sum+=dy[b.m]-b.d;
    			b.m++;
    			for(int i=b.m;i<a.m;i++)
    			{
    				if(leap(b.y))sum+=ey[i];
    				else sum+=dy[i];
    			}
    			sum+=a.d;
    		}
    	}
    	else
    	{
    		if(leap(b.y))//如果b是闰年 
    		{
    			sum+=ey[b.m]-b.d;
    			b.m++;
    			for(int i=b.m;i<=12;i++)sum+=ey[i];
    		} 
    		else 
    		{
    			sum+=dy[b.m]-b.d;
    			b.m++;
    			for(int i=b.m;i<=12;i++)sum+=dy[i];
    		} //把b这年补齐 
    		for(int i=b.y+1;i<a.y;i++)//从b的下一年开始 到a的前一年 
    		{
    			if(leap(i))sum+=366;
    			else sum+=365;
    		}
    		if(leap(a.y))
    		{
    			sum+=a.d;
    			a.m--;
    			for(int i=1;i<=a.m;i++)sum+=ey[i];
    		}
    		else
    		{
    			sum+=a.d;
    			a.m--;
    			for(int i=1;i<=a.m;i++)sum+=dy[i];
    		}
    	}	
    	return sum;
    }
    int main()
    {
    	scanf("%d:%d:%d
    %d:%d:%d",&a.y,&a.m,&a.d,&b.y,&b.m,&b.d);
    	cout<<abs(deal_data(a,b));
    	return 0;
    }
    
  • 相关阅读:
    Python什么是二次开发的意义?python在.net项目采用
    斐波那契数
    Java 兑换ObjectC代码
    第18本:《整理的艺术》
    第16本:《视觉繁美:信息可视化方法与案例解析》
    第14本:《李鸿章传》
    第10本:《设计心理学》
    第17本:《代码的未来》
    第15本:《视不可当----信息图与可视化传播》
    第13本:《富爸爸,穷爸爸》
  • 原文地址:https://www.cnblogs.com/LiangYC1021/p/12990385.html
Copyright © 2011-2022 走看看