HDU4545+计算日期

时间:2022-06-26 16:31:12
/*
计算过了D天后的日期
之前D天的日期
*/
#include<stdio.h>
int judge_year( int year ){
if( (year%4==0&&year%100!=0)||(year%400==0) )
return true;
else
return false;
}
int judge_month( int mon ){
if( mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12 )
return true;
else
return false;
}
int main(){
int ca;
//freopen("cmp.txt","w",stdout);
scanf("%d",&ca);
while( ca-- ){
int n;
scanf("%d",&n); int year,mon,day;
year = 2013;
mon = 3;
day = 24;
int sum = n;
while( sum>0 ){
if( judge_year(year)==true ){ //printf("sum=%d,,year = %d,mon=%d,day=%d\n",sum,year,mon,day);
if( mon==2 ){
int left = 29-day;
if( sum>left ){
sum = sum-(left+1);
mon++;
day = 1;
}
else if( sum==left ){
sum = 0;
day = 29;
}
else {
day += sum;
sum = 0;
}
}
else if( judge_month( mon )==true ){
int left = 31-day;
if( sum>left ){
sum = sum-left-1;
mon++;
if( mon>12 ) {
year++;
mon = 1;
}
day = 1;
}
else if( sum==left ){
sum = 0;
day = 31;
}
else{
day += sum;
sum = 0;
}
}
else {
int left = 30-day;
if( sum>left ){
sum = sum-left-1;
mon++;
if( mon>12 ) {
year++;
mon = 1;
}
day = 1;
}
else if( sum==left ){
sum = 0;
day = 30;
}
else{
day += sum;
sum = 0;
}
} }//闰年
else{ if( mon==2 ){
int left = 28-day;
if( sum>left ){
sum = sum-left-1;
mon++;
day = 1;
}
else if( sum==left ){
sum = 0;
day = 28;
}
else {
day += sum;
sum = 0;
}
}
else if( judge_month( mon )==true ){
int left = 31-day;
if( sum>left ){
sum = sum-left-1;
mon++;
if( mon>12 ) {
year++;
mon = 1;
}
day = 1;
}
else if( sum==left ){
sum = 0;
day = 31;
}
else{
day += sum;
sum = 0;
}
}
else {
int left = 30-day;
if( sum>left ){
sum = sum-left-1;
mon++;
if( mon>12 ) {
mon = 1;
year++;
}
day = 1;
}
else if( sum==left ){
sum = 0;
day = 30;
}
else{
day += sum;
sum = 0;
}
} }//不是闰年
if( sum<=0 )
break;
}//向后D天的日期 printf("%04d/%02d/%02d ",year,mon,day);
year = 2013;
mon = 3;
day = 24;
sum = n;
while( sum>0 ){
if( judge_year( year )==true ){
if( sum>=366 ){
sum-=366;
year--;
}
else{
if( mon==3 ){
int left = day;
if( sum>=29 ){
sum-=29;
mon--;
}
else {
if( sum==left-1 ){
sum = 0;
day = 1;
}
else if( sum<left-1 ){
day -= sum;
sum = 0;
}
else {
sum -= left;
day = 29;
mon--;
}
}
}
else if( mon==5||mon==7||mon==10||mon==12 ){
int left = day;
if( sum>=30 ){
sum-=30;
mon--;
}
else {
if( sum==left-1 ){
sum = 0;
day = 1;
}
else if( sum<left-1 ){
day -= sum;
sum = 0;
}
else {
sum -= left;
day = 30;
mon--;
}
}
}
else {
int left = day;
if( sum>=31 ){
sum-=31;
mon--;
if( mon<=0 ){
year--;
mon=12;
}
}
else {
if( sum==left-1 ){
sum = 0;
day = 1;
}
else if( sum<left-1 ){
day -= sum;
sum = 0;
}
else {
sum -= left;
day = 31;
mon--;
if( mon==0 ){
mon = 12;
year--;
}
}
}
}
}
}//闰年
else {
if( sum>=365 ){
sum-=365;
year--;
}
else{
if( mon==3 ){
int left = day;
if( sum>=28 ){
sum-=28;
mon--;
}
else {
if( sum==left-1 ){
sum = 0;
day = 1;
}
else if( sum<left-1 ){
day -= sum;
sum = 0;
}
else {
sum -= left;
day = 28;
mon--;
}
}
}
else if( mon==5||mon==7||mon==10||mon==12 ){
int left = day;
if( sum>=30 ){
sum-=30;
mon--;
}
else {
if( sum==left-1 ){
sum = 0;
day = 1;
}
else if( sum<left-1 ){
day -= sum;
sum = 0;
}
else {
sum -= left;
day = 30;
mon--;
}
}
}
else {
int left = day;
if( sum>=31 ){
sum-=31;
mon--;
if( mon==0 ){
year--;
mon=12;
}
}
else {
if( sum==left-1 ){
sum = 0;
day = 1;
}
else if( sum<left-1 ){
day -= sum;
sum = 0;
}
else {
sum -= left;
day = 31;
mon--;
if( mon==0 ){
mon = 12;
year--;
}
}
}
}
}
}//不是闰年
if( sum<=0 ) break;
} printf("%04d/%02d/%02d\n",year,mon,day);
}
return 0;
}