HDU4515+计算日期
模拟!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 计算过了D天后的日期 3 之前D天的日期 4 */ 5 #include<stdio.h> 6 int judge_year( int year ){ 7 if( (year%4==0&&year%100!=0)||(year%400==0) ) 8 return true; 9 else 10 return false; 11 } 12 int judge_month( int mon ){ 13 if( mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12 ) 14 return true; 15 else 16 return false; 17 } 18 int main(){ 19 int ca; 20 //freopen("cmp.txt","w",stdout); 21 scanf("%d",&ca); 22 while( ca-- ){ 23 int n; 24 scanf("%d",&n); 25 26 int year,mon,day; 27 year = 2013; 28 mon = 3; 29 day = 24; 30 int sum = n; 31 while( sum>0 ){ 32 if( judge_year(year)==true ){ 33 34 //printf("sum=%d,,year = %d,mon=%d,day=%d\n",sum,year,mon,day); 35 if( mon==2 ){ 36 int left = 29-day; 37 if( sum>left ){ 38 sum = sum-(left+1); 39 mon++; 40 day = 1; 41 } 42 else if( sum==left ){ 43 sum = 0; 44 day = 29; 45 } 46 else { 47 day += sum; 48 sum = 0; 49 } 50 } 51 else if( judge_month( mon )==true ){ 52 int left = 31-day; 53 if( sum>left ){ 54 sum = sum-left-1; 55 mon++; 56 if( mon>12 ) { 57 year++; 58 mon = 1; 59 } 60 day = 1; 61 } 62 else if( sum==left ){ 63 sum = 0; 64 day = 31; 65 } 66 else{ 67 day += sum; 68 sum = 0; 69 } 70 } 71 else { 72 int left = 30-day; 73 if( sum>left ){ 74 sum = sum-left-1; 75 mon++; 76 if( mon>12 ) { 77 year++; 78 mon = 1; 79 } 80 day = 1; 81 } 82 else if( sum==left ){ 83 sum = 0; 84 day = 30; 85 } 86 else{ 87 day += sum; 88 sum = 0; 89 } 90 } 91 92 }//闰年 93 else{ 94 95 if( mon==2 ){ 96 int left = 28-day; 97 if( sum>left ){ 98 sum = sum-left-1; 99 mon++; 100 day = 1; 101 } 102 else if( sum==left ){ 103 sum = 0; 104 day = 28; 105 } 106 else { 107 day += sum; 108 sum = 0; 109 } 110 } 111 else if( judge_month( mon )==true ){ 112 int left = 31-day; 113 if( sum>left ){ 114 sum = sum-left-1; 115 mon++; 116 if( mon>12 ) { 117 year++; 118 mon = 1; 119 } 120 day = 1; 121 } 122 else if( sum==left ){ 123 sum = 0; 124 day = 31; 125 } 126 else{ 127 day += sum; 128 sum = 0; 129 } 130 } 131 else { 132 int left = 30-day; 133 if( sum>left ){ 134 sum = sum-left-1; 135 mon++; 136 if( mon>12 ) { 137 mon = 1; 138 year++; 139 } 140 day = 1; 141 } 142 else if( sum==left ){ 143 sum = 0; 144 day = 30; 145 } 146 else{ 147 day += sum; 148 sum = 0; 149 } 150 } 151 152 }//不是闰年 153 if( sum<=0 ) 154 break; 155 }//向后D天的日期 156 157 printf("%04d/%02d/%02d ",year,mon,day); 158 year = 2013; 159 mon = 3; 160 day = 24; 161 sum = n; 162 while( sum>0 ){ 163 if( judge_year( year )==true ){ 164 if( sum>=366 ){ 165 sum-=366; 166 year--; 167 } 168 else{ 169 if( mon==3 ){ 170 int left = day; 171 if( sum>=29 ){ 172 sum-=29; 173 mon--; 174 } 175 else { 176 if( sum==left-1 ){ 177 sum = 0; 178 day = 1; 179 } 180 else if( sum<left-1 ){ 181 day -= sum; 182 sum = 0; 183 } 184 else { 185 sum -= left; 186 day = 29; 187 mon--; 188 } 189 } 190 } 191 else if( mon==5||mon==7||mon==10||mon==12 ){ 192 int left = day; 193 if( sum>=30 ){ 194 sum-=30; 195 mon--; 196 } 197 else { 198 if( sum==left-1 ){ 199 sum = 0; 200 day = 1; 201 } 202 else if( sum<left-1 ){ 203 day -= sum; 204 sum = 0; 205 } 206 else { 207 sum -= left; 208 day = 30; 209 mon--; 210 } 211 } 212 } 213 else { 214 int left = day; 215 if( sum>=31 ){ 216 sum-=31; 217 mon--; 218 if( mon<=0 ){ 219 year--; 220 mon=12; 221 } 222 } 223 else { 224 if( sum==left-1 ){ 225 sum = 0; 226 day = 1; 227 } 228 else if( sum<left-1 ){ 229 day -= sum; 230 sum = 0; 231 } 232 else { 233 sum -= left; 234 day = 31; 235 mon--; 236 if( mon==0 ){ 237 mon = 12; 238 year--; 239 } 240 } 241 } 242 } 243 } 244 }//闰年 245 else { 246 if( sum>=365 ){ 247 sum-=365; 248 year--; 249 } 250 else{ 251 if( mon==3 ){ 252 int left = day; 253 if( sum>=28 ){ 254 sum-=28; 255 mon--; 256 } 257 else { 258 if( sum==left-1 ){ 259 sum = 0; 260 day = 1; 261 } 262 else if( sum<left-1 ){ 263 day -= sum; 264 sum = 0; 265 } 266 else { 267 sum -= left; 268 day = 28; 269 mon--; 270 } 271 } 272 } 273 else if( mon==5||mon==7||mon==10||mon==12 ){ 274 int left = day; 275 if( sum>=30 ){ 276 sum-=30; 277 mon--; 278 } 279 else { 280 if( sum==left-1 ){ 281 sum = 0; 282 day = 1; 283 } 284 else if( sum<left-1 ){ 285 day -= sum; 286 sum = 0; 287 } 288 else { 289 sum -= left; 290 day = 30; 291 mon--; 292 } 293 } 294 } 295 else { 296 int left = day; 297 if( sum>=31 ){ 298 sum-=31; 299 mon--; 300 if( mon==0 ){ 301 year--; 302 mon=12; 303 } 304 } 305 else { 306 if( sum==left-1 ){ 307 sum = 0; 308 day = 1; 309 } 310 else if( sum<left-1 ){ 311 day -= sum; 312 sum = 0; 313 } 314 else { 315 sum -= left; 316 day = 31; 317 mon--; 318 if( mon==0 ){ 319 mon = 12; 320 year--; 321 } 322 } 323 } 324 } 325 } 326 }//不是闰年 327 if( sum<=0 ) break; 328 } 329 330 printf("%04d/%02d/%02d\n",year,mon,day); 331 } 332 return 0; 333 }
keep moving...