一次就过的日期问题得纪念一下,感谢几组测试数据
加法的测试数据: 38 39 282 283 7 8 减法测试数据: 52 87345 24 23 100000
答案在Excel中测试吧,主要是很多细节性的问题要考虑到。
#include<iostream>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;
int md[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int leap(int n)
{
if((n%4==0&&n%100!=0)||(n%400==0))
return 1;
return 0;
}
void fun1(int d)
{
if(d<=7) printf("2013/03/%02d ",24+d);
else{
d = d-7;
int year = 2013,month = 4;
int tmp,flag= 0;
while(d>0)
{
tmp = d;
if(month == 2&&flag==1&&d-md[month]-1>0)
d = d-md[month++]-1;
else if(d-md[month]>0)
d = d-md[month++];
else break;
if(month>12){
month = 1;
year++;
if(leap(year)==1)
flag = 1;
else flag = 0;
}
}
printf("%d/%02d/%02d ",year,month,tmp);
}
}
void fun2(int d)
{
if(d<24) printf("2013/03/%02d\n",24-d);
else{
d = d-24;
int year=2013,month=2;
int flag = 0,tmp;
while(d>0)
{
if(month==2&&flag==1&&d-md[month]-1>=0)
d = d-md[month--]-1;
else if(d-md[month]>=0)
d = d-md[month--];
else break;
if(month<1)
{
year--;
if(leap(year)==1) flag=1;
else flag=0;
month = 12;
}
}
printf("%d/%02d/%02d\n",year,month,md[month]-d);
}
}
int main()
{
int T,d;cin>>T;
while(T--)
{
cin>>d;
fun1(d);
fun2(d);
}
return 0;
}