HDOJ 4515 小Q系列故事——世界上最遥远的距离
1 #include<stdlib.h> 2 #include<stdio.h> 3 int year,month,day; 4 int m[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31, 5 0,31,29,31,30,31,30,31,31,30,31,30,31}; 6 int y[660]={0}; 7 bool isrun(int year) 8 { 9 if((year%4==0&&year%100!=0)||year%400==0) 10 return 1; 11 return 0; 12 } 13 void fm(int len) 14 { 15 if(len<283){ 16 int i=4; 17 if(len>7) len-=7; 18 else { 19 i=3; 20 len+=24; 21 } 22 while(len>m[0][i]){ 23 len-=m[0][i]; 24 i++; 25 } 26 year=2013; 27 month=i; 28 day=len; 29 }else{ 30 int i=2013-1700; 31 len-=283; 32 i++; 33 i+=1700; 34 for(;isrun(i)?len>=366:len>=365;len-=365+isrun(i),i++); 35 year=i; 36 for(i=1;len&&len>=m[isrun(year)][i];len-=m[isrun(year)][i],i++); 37 38 day=len+1; 39 month=i; 40 } 41 printf("%d/%02d/%02d ",year,month,day); 42 } 43 void fw(int len) 44 { 45 if(len<83){ 46 int i=3; 47 if(len>24){ 48 len-=24; 49 i--; 50 while(len>m[0][i]){ 51 len-=m[0][i]; 52 i--; 53 } 54 day=m[0][i]-len; 55 } 56 else day=24-len; 57 year=2013; 58 month=i; 59 }else{ 60 int i=2013-1700; 61 len-=82; 62 i--; 63 while(len>y[i]){ 64 len-=y[i]; 65 i--; 66 } 67 year=i+1700; 68 i=12; 69 while(len>m[isrun(year)][i]){ 70 len-=m[isrun(year)][i]; 71 i--; 72 } 73 day=m[isrun(year)][i]-len+1; 74 month=i; 75 } 76 printf("%d/%02d/%02d\n",year,month,day); 77 } 78 int main() 79 { 80 int i,N,len; 81 for(i=0;i<650;i++) 82 if(isrun(i+1700)) 83 y[i]=366; 84 else 85 y[i]=365; 86 87 scanf("%d",&N); 88 while(N--) 89 { 90 scanf("%d",&len); 91 fm(len); 92 fw(len); 93 } 94 //system("pause"); 95 return 0; 96 }