calculate actual time的经典复杂方式,记忆犹新,还是想贴出来

睁着眼做梦 posted @ 2013年11月14日 19:25 in Java , 1007 阅读
private void calculateTime(int month,int year){ i=true; while (i==true){/* The logic is a loop using.*/ if(month==4||month==6||month==9||month==11){ // If the month only has 30 days,but the day greater than 30, if(new_d>30){ new_d=new_d-30;month++; // then the day should minus 30 and the month should add 1. }else i=false; }else if(month==2){ if ((year%4==0&&year%100!=0)||(year%400==0&&year%3200!=0)){ /* The leap year and in Febuary.*/ if(new_d>29){ /* Febuary will has 29 days.*/ new_d=new_d-29;month++; }else i=false; }else{ /* Febuary in a normal year.*/ if(new_d>28){ /* Febuary only has 28 days.*/ new_d=new_d-28;month++; }else i=false; } }else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){ if(new_d>31) { new_d=new_d-31;month++; }else i=false; } new_m=month; if(month>12){ //If month is greater than 12, new_y=year+month/12; //both the year and the month will be changed. new_m=month%12; month=new_m; year=new_y; } } //The loop can ensure that the month and the year keep changing until the day is in a correct range. }
 
PS. 到现在还是想不明白,当时为了计算正确的叠加后的月份与日子,尽然想了很久,最后还是借鉴了别人的答案。不过就是一步一步得叠加秒与分钟,叠加到月的时候用个循环罢了,为什么当时就觉得那么复杂呢?
由此我又要检讨自己了。这是一个相当正常的思路,quote
 
if(month>12){              //If month is greater than 12,
new_y=year+month/12;   //both the year and the month will be changed.
 
可是我却想了很久,就是因为我没有把思路理清,为何没有理清呢?就是源于没有把我觉得复杂的可能性列在纸上,连笔都不愿意动的人,怎么能够独立思考一道题,更不要说得到最终的个人最佳答案,我该狠狠得反省了。很多结果的生成并不是因为实力的不足够,而是源于信心的不充足,对自己的不信任,不能够冷静得把逻辑理清楚,是浮躁了!成功是需要静下心来想办法达到的,这次冒险过了,下次就不能luck out。
 
谨以此文逼迫自己谨记假期梦中的haunting headache。
 
 

 

 

 

 

 

 

  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter