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;
    }
    
  • 相关阅读:
    Microsoft SQL Server 2008 技术内幕:TSQL查询 逻辑查询处理阶段(一)
    我学Delphi心得及笔记Variant 数据类型无类型(第八讲)
    Microsoft SQL Server 2008 技术内幕:TSQL查询 笔记(一)
    我学Delphi心得及笔记字符串操作(第六讲)
    Delphi 之弹出气泡消息提示
    我学Delphi心得及笔记过程与函数(第五讲)
    我学Delphi心得及笔记面向对象系列(多态)
    我学Delphi心得及笔记第一个程序 Hello World ! (第十讲)
    我学Delphi心得及笔记用户自定义数据类型(第三讲)
    我学Delphi心得及笔记内存(第七讲)
  • 原文地址:https://www.cnblogs.com/LiangYC1021/p/12990385.html
Copyright © 2011-2022 走看看