jobdu 6 日期差值

题目描述:

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入:

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出:

每组数据输出一行,即日期差值

样例输入:
20110412
20110422
样例输出:

11

 code1:

#include <stdio.h>
int R[]={0,31,59,90,120,151,181,212,243,273,304,334};
int calc(int date)
{
	int year=date/10000;
	int month=date%10000/100;
	int day=date%100;
	int r=R[month-1]+day;
	int x=0;
	for(int i=1;i<year;i++)
	{
		if(((i%4==0)&&(i%100!=0))||(i%400==0))
			x++;
	}
	r+=x+(year-1)*365;
	if(((year%4==0)&&(year%100!=0))||(year%400==0)&&month>2)
		r++;
	return r;
}
int abs(int m)
{
	return m<0?-m:m;
}
int main()
{
	int date1,date2;
	while(scanf("%d%d",&date1,&date2)!=EOF)
		printf("%d\n",abs(calc(date2)-calc(date1))+1);
	return 0;
}


code2:

#include <stdio.h>
#define ISYEAP(x) x%100!=0&&x%4==0||x%400==0?1:0
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++;
			}
		}
	}
};
int buf[5001][13][32];
int abs(int x)
{
	return x<0?-x:x;
}
int main()
{
	Date tmp;
	tmp.Year=0;
	tmp.Month=1;
	tmp.Day=1;
	int cnt=0;
	while(tmp.Year!=5001)
	{
		buf[tmp.Year][tmp.Month][tmp.Day]=cnt;
		tmp.nextDay();
		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\n",abs(buf[y2][m2][d2]-buf[y1][m1][d1])+1);		
	}
	return 0;	
}


 

 

你可能感兴趣的:(jobdu 6 日期差值)