100 Door Puzzle
问题重述:
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的更多相关文章
- Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)
题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广 ...
- POJ1651Multiplication Puzzle[区间DP]
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8737 Accepted: ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- Ignatius's puzzle
Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- POJ1651Multiplication Puzzle(矩阵链乘变形)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8040 Accepted: ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- HDOJ 1098 Ignatius's puzzle
Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...
- 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) ...
- codeforces A. Orchestra B. Island Puzzle
A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- JVM 垃圾回收器工作原理及使用实例介绍
IBM介绍文档:https://www.ibm.com/developerworks/cn/java/j-lo-JVMGarbageCollection/ Java 的新生代串行垃圾回收器中使用了复制 ...
- es let2
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ajax批删
- linux c 获取头文件函数getenv
#include <stdio.h>#include <stdlib.h> int main(){ printf("%s\n", getenv(" ...
- mysql临时表的产生
sql执行会生成一个巨大的临时表,当内存放不下时,要全部copy 到磁盘,导致IO飙升,时间开销增大. 额外收获知识收藏如下: 临时表存储 MySQL临时表分为"内存临时表"和&q ...
- Nexus3.0.0+Maven的使用(二)
因为Nexus3.0.0与Nexus2.X系列的差别很大,所以本章节我大概讲解下Nexus3.0.0的功能使用. 1.功能介绍 1.1 Browse Server Content 1.1.1 Se ...
- ubuntu下编译protobuf
参考: http://blog.csdn.net/wuzuyu365/article/details/51900676 1.下载protobuf下载地址:https://github.com/goog ...
- CentOS7:安装Zabbix
参考:CentOS 7 yum安装Zabbix 1. 安装Zabbix Server EPEL源里面有Zabbix的安装包,所以需要先安装EPEL. Zabbix源也可以从这里获得:http://re ...
- gradle修改AndroidManifest.xml中的版本号
def VersionCode = "19" ant.replaceregexp(file:"../Assets/Plugins/Android/AndroidManif ...
- 如何查看屏幕touch driver是否在正常工作
1. adb shell cat proc/bus/input/devices查看touch对应的是哪个event,如是event3: 2. adb shell getevent dev/input/ ...