Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220913
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-09 10:55
个人简介

每天改变一点点,生活充满了惊喜。

文章分类

全部博文(42)

文章存档

2016年(8)

2015年(29)

2014年(5)

我的朋友

分类: PHP

2016-02-16 14:58:45

今天碰到一个比较有趣的问题:
从前有一位老钟表匠,为一个教堂装一只大钟。他年老眼花,把长短针装配错了,短针走的速度反而是长针的12倍。
装配的时候是上午6点,他把短针指在“6”上,长针指在“12”上。老钟表匠装好就回家去了。
人们看这钟一会儿7点,过了不一会儿就8点了,都很奇怪,立刻去找老钟表匠。
等老钟表匠赶到,已经是下午7点多钟。他掏出怀表来一对,钟准确无误,疑心人们有意捉弄他,一生气就回去了。
这钟还是8点、9点地跑,人们再去找钟表匠。老钟表匠第二天早晨8点多赶来用表一对,仍旧准确无误。
请你想一想,老钟表匠第一次对表的时候是7点几分?第二次对表又是8点几分?
A. 35 42
B. 26 35
C. 38 44
D. 05 10

解决这个问题的代码:
  1. <?
  2. class clock{
  3.     const TIME_SCALE = 60;
  4.     const DAY_SCALE = 12;
  5.     private $correct_time_hour;
  6.     private $correct_time_min;
  7.     private $clock_time_hour;
  8.     private $clock_time_min;
  9.     private $clock_time;
  10.     private $am_or_pm;

  11.     public function __construct(){
  12.         $this->correct_time_hour = 6;
  13.         $this->correct_time_min = 0;
  14.         $this->clock_time_hour = 0;
  15.         $this->clock_time_min = 30;
  16.         $this->clock_time_display_hour = null;
  17.         $this->clock_time_display_min = null;
  18.         $this->am_or_pm = 1;
  19.     }
  20.     public function main(){
  21.         do {
  22.             $this->correct_time_min = $this->correct_time_min + 1;
  23.             if ($this->correct_time_min >= 60){
  24.                 $this->correct_time_min = $this->correct_time_min - 60;
  25.                 $this->correct_time_hour++;
  26.                 if ($this->correct_time_hour >= 12){
  27.                     $this->correct_time_hour = $this->correct_time_hour - 12;
  28.                     $this->am_or_pm++;
  29.                 }
  30.             }
  31.             $this->clock_time_hour = $this->clock_time_hour + 1;
  32.             if ($this->clock_time_hour >= 60){
  33.                 $this->clock_time_hour = $this->clock_time_hour - 60;
  34.             }
  35.             if ( $this->clock_time_hour % 12 === 0 ){
  36.                 $this->clock_time_min++;
  37.                 if ($this->clock_time_min >= 60){
  38.                     $this->clock_time_min = $this->clock_time_min - 60;
  39.                 }
  40.             }
  41.             $clock_time = $this->getClockTime();
  42.             if ($this->isFirstDayPmSeven()){
  43.                 printf("First day 7 PM time : %s:%s\n", $this->correct_time_hour, $this->correct_time_min);
  44.             }
  45.             if ($this->isSecondDayAmEight()){
  46.                 printf("Second day 8 AM time : %s:%s\n", $this->correct_time_hour, $this->correct_time_min);
  47.                 break;
  48.             }
  49.             if ($this->isTimeCorrect()){
  50.                 printf("Time correct when : %s:%s\n", $this->correct_time_hour, $this->correct_time_min);
  51.             }
  52.             if ($this->dayNo() > 2){
  53.                 echo 'error : has bug' . "\n";
  54.                 break;
  55.             }
  56.         } while (true);
  57.         return true;
  58.     }
  59.     private function isFirstDayPmSeven(){
  60.         if (($this->dayNo() === 1) && $this->isPM()
  61.             && ($this->correct_time_hour === 7) && ($this->clock_time_display_hour === 7)
  62.             && ($this->correct_time_min === $this->clock_time_display_min)){
  63.             return true;
  64.         }
  65.         return false;
  66.     }
  67.     private function isSecondDayAmEight(){
  68.         if (($this->dayNo() === 2) && $this->isAM()
  69.             && ($this->correct_time_hour === 8) && ($this->clock_time_display_hour === 8)
  70.             && ($this->correct_time_min === $this->clock_time_display_min)){
  71.             return true;
  72.         }
  73.         return false;
  74.     }
  75.     private function isTimeCorrect(){
  76.         if (($this->correct_time_hour === $this->clock_time_display_hour)
  77.             && ($this->correct_time_min === $this->clock_time_display_min)){
  78.             return true;
  79.         }
  80.         return false;
  81.     }
  82.     private function dayNo(){
  83.         return intval(ceil($this->am_or_pm / 2));
  84.     }
  85.     private function isPM(){
  86.         return (($this->am_or_pm % 2) === 0) ? true : false;
  87.     }
  88.     private function isAM(){
  89.         return (($this->am_or_pm % 2) === 1) ? true : false;
  90.     }
  91.     private function getClockTime(){
  92.         $this->clock_time_display_hour = intval(floor($this->clock_time_hour / 5));
  93.         $this->clock_time_display_min = $this->clock_time_min;
  94.     }
  95.     private function outputTime($hour, $min){
  96.         printf('%s:%s', $hour, $min);
  97.     }
  98. }//class

  99. $objClock = new clock();
  100. $objClock->main();

执行输出:
Time correct when : 6:32
Time correct when : 7:38
Time correct when : 8:43
Time correct when : 9:49
Time correct when : 10:54
Time correct when : 11:59
Time correct when : 0:0
Time correct when : 1:5
Time correct when : 2:10
Time correct when : 3:16
Time correct when : 4:21
Time correct when : 5:27
Time correct when : 6:32
First day 7 PM time : 7:38
Time correct when : 7:38
Time correct when : 8:43
Time correct when : 9:49
Time correct when : 10:54
Time correct when : 11:59
Time correct when : 0:0
Time correct when : 1:5
Time correct when : 2:10
Time correct when : 3:16
Time correct when : 4:21
Time correct when : 5:27
Time correct when : 6:32
Time correct when : 7:38
Second day 8 AM time : 8:43

红色字体部分是问题的答案,会发现答案和选项存在误差,选项中的答案是44,而输出的是43。
这是由于代码中对于 分钟采用的是向下取整的处理方式,如果按照四舍五入的方式,可以得到和选项一致的答案。

输出中还打印出了表的展示的时间和实际的时间一致的时间点,可以发现每过1小时,5分钟或6分钟(精度的原因,实际上是具有规律的相同值),
表的展示的时间和实际的时间一致就会发生一次。
阅读(2117) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~