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/

时间: 2024-11-10 11:09:23

100 door puzzle的相关文章

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

题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广,例如8-puzzle是3*3,程序要能解决n*n的同类问题(2 ≤ n < 128) 典型的8 puzzle如下: 算法设计参照A*搜索算法,即使不了解A*搜索算法,题目也已经将解法解释的很具体了. Best-first search:设计参照A* 搜索算法.定义一个 search node类,包

Eggs Dropping puzzle(2 eggs, 100 floors)

题目如下: You are given two eggs, and access to a 100-storey building. Both eggs are identical. The aim is to find out the highest floor from which an egg will not break when dropped out of a window from that floor. If an egg is dropped and does not brea

[Puzzle] 5 Pirates and 100 Gold Coins

https://wesleydeng.iteye.com/blog/1186457 https://www.geeksforgeeks.org/puzzle-5-finding-the-poisoned-wine/ 原文地址:https://www.cnblogs.com/lawrenceSeattle/p/10281548.html

UVa - 227 - Puzzle

给空格子上下左右的互换操作,问最后是怎样的 注意一行的最后一个若是空格,需要自己加注意读取时 操作可能分好多行,一定要读取到 0 为止 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 char map[50][50],op[1000],c,tmp; 5 int k,t,x,y,cnt; 6 bool flag; 7 void fuc() 8 { 9 flag=1; 10 for(int i=0;

Aizu 0121 Seven Puzzle (康托展开+bfs)

Seven Puzzle Time Limit : 1 sec, Memory Limit : 65536 KB 7パズルは8つの正方形のカードとこれらのカードがぴたりと収まる枠を使って行います.それぞれのカードは互いに区別できるように.0,1,2....7と番号がつけられています.枠には.縦に2個.横に4個のカードを並べることができます. 7パズルを始めるときには.まず枠にすべてのカードを入れます.枠のなかで0のカードだけは.上下左右に隣接するカードと位置を交換することができます.たとえば.枠

POJ1651——Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: 3964 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

hdu-1538 A Puzzle for Pirates

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1538 题目类型: 模拟 题意概括: 有1~n,n个海盗,m块金子,第n个海盗要提出一个分金方案,如果有一半以上的人同意,就立刻分金,反之就将这个人扔下水里,问所有情况都是最优解的情况下,要给第p个海盗分多少金? 解题思路: 如果只有一个人,所以肯定是给自己. 如果有两个人,那么自己将所有的钱都给自己,那么自己也同意分配,就可以得到一半以上的人的同意. 如果有三个人,第一个人知道如果不同意三号,那么

poj1651Multiplication Puzzle(取数,区间DP)

Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move