问题重述:

There are 100 doors in a long hallway. They are all closed. The first time you walk by each door, you open it. The second time around, you close every second door (since they are all opened). On the third pass you stop at every third door and open it if it’s closed, close it if it’s open. On the fourth pass, you take action on every fourth door. You repeat this pattern for 100 passes.

Question: At the end of 100 passes, what doors are opened and what doors are closed?

分析与解答:

这个问题没想通的觉得不难但是很繁,想通了觉得既不难也不繁。主要的想法就是说某编号的门如果被经过了奇数次,则门的状态与起始状态相反;如果被经过了偶数次,则门的状态与起始状态相同。

那么通用的做法是看该门编号对应的数能被多少数整除(除数要小于趟数)。如果除数个数是奇数则判定门的状态为起始状态的相反状态;否则,判定门的状态为起始状态。

这边,要不要这么做呢?wait……马克思爷爷曾经教导我们说:“具体问题具体分析!”。这个问题有什么具体情况呢?那就是:本题的趟数是和门数一致的。也就是说,只要看门的编号能被多少数整除就行了,不必担心需要除数小于等于趟数的限制条件。这样,我们就可以有一个条件了:那就是某个数的两个因子都在限制范围内。我们又发现,只要某个数能表示成两个因子的乘积,而这两个因子又互不相同,那么这两趟算是白跑啦,对门的状态没有影响。那么对门的状态有影响的,就是那两个因子相同的情况。这样,问题的解就是哪些编号为完全平方数的门啦!!!

自此,答案显而易见,状态有变的门的编号为:1,4,9,16,25,36,49,64,81,100。

参考文献:

1.        http://classic-puzzles.blogspot.com/2008/05/door-toggling-puzzle-or-100-doors.html

2.        http://www.theodorenguyen-cao.com/2008/02/02/puzzle-100-doors/

100 Door Puzzle的更多相关文章

  1. Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)

    题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广 ...

  2. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  3. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  4. Ignatius's puzzle

    Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. POJ1651Multiplication Puzzle(矩阵链乘变形)

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8040   Accepted: ...

  6. POJ1651:Multiplication Puzzle(区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  7. HDOJ 1098 Ignatius's puzzle

    Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...

  8. UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>

    F - Eight Puzzle Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) ...

  9. codeforces A. Orchestra B. Island Puzzle

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. VES Hand Book Contents

    3...ABOUT THE VES4...Foreword 6...Chapter 1......Introduction6......Visual Effects and Special Effec ...

  2. ln命令小陷阱

    一个文件夹下面的文件结构是 -dir1 -assetsdir -subdir1 -subdir2 这个时候如果我想在subdir1和subdir2下面分别创建assetsdir的链接文件夹的话,应该怎 ...

  3. 基于Redis、Storm的实时数据查询实践

    通过算法小组给出的聚合文件,我们需要实现一种业务场景,通过用户的消费地点的商户ID与posId,查询出他所在的商圈,并通过商圈地点查询出与该区域的做活动的商户,并与之进行消息匹配,推送相应活动信息到用 ...

  4. printf()输出

    printf()函数是式样化输出函数, 一般用于向准则输出设备按规定式样输出消息.正在编写步骤时经常会用到此函数.printf()函数的挪用式样为: printf("<式样化字符串&g ...

  5. (转)发布Silverlight+WCF程序到IIS后,客户端访问数据库失败的解决方案

    转自url:http://greatverve.cnblogs.com/archive/2011/11/30/silverlight-wcf-pub.html 我们在编写Silverlight程序时, ...

  6. .net 制作二维码

    首先  要下载  ThoughtWorks.QRCode.dll 下载地址:http://download.csdn.net/detail/lybwwp/6861821 然后新建项目 把Thought ...

  7. SICP练习记录

    -------------求一个数的平方根(牛顿法平方根求解法): (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt ...

  8. 【JAVA】【Eclipse】出现This element neither has attached source nor attached Javadoc...的解决方法

    This element neither has attached source nor attached Javadoc and hence no Javadoc could be found Ec ...

  9. 1.使用Entity Framwork框架常用的技术手段Code First 和Reverse Engineer Code First

    提示:VS版本2013,  Entity Framwork版本5.0.0,Mysql数据库  使用Entity FrameWork的好处就不多说,直接上手如何使用.两种形式:1.将代码映射到数据库实体 ...

  10. ZYNQ 的PS GEM DMA存在缺陷

    使用iperf对zynq进行单socket tcp传输速率测试: 无网络损伤时,单向网络带宽约为600Mbps,双向网络带宽相加约400Mbps: 50ms延时,1ms抖动,无丢包时,单向网络带宽约为 ...