Question

There are 100 doors in a row that are all initially closed.

You make 100 passes by the doors.

The first time through, visit every door and toggle the door (if the door is closed,open it;if it is open,close it).

The second time, only visit every 2nd door (door #2, #4, #6, ...),and toggle it.

The third time, visit every 3rd door (door #3, #6, #9, ...), etc,until you only visit the 100th door.

Task

Answer the question: what state are the doors in after the last pass? Which are open, which are closed?

先把问题缩小: 10道门关着,按以上方式遍历这些门10次。 最后很明显,第一道们肯定是开着的;第二、第三是关着的! X道门的开关次数跟X的约数有关,如果X有偶数个约数则门最终的状态是关着的,否则为开着的。

问题现在变成: 1 到 100 之间公约数个数为奇数的数字是?

[i * i for i in range(1, int(math.sqrt(100)) + 1)] // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Java Solution

public class Doors
{
public static void main(String[] args)
{
for(int i=0;i<10;i++)
System.out.println("Door #"+(i + 1)*(i + 1) +" is open.");
}
}

100 doors的更多相关文章

  1. 100 Door Puzzle

    问题重述: There are 100 doors in a long hallway. They are all closed. The first time you walk by each do ...

  2. 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏

    Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...

  3. POJ 1300 Door Man - from lanshui_Yang

    Description You are a butler in a large mansion. This mansion has so many rooms that they are merely ...

  4. POJ1300(欧拉回路)

    Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2139   Accepted: 858 Descripti ...

  5. POJ 1300.Door Man 欧拉通路

    Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2596   Accepted: 1046 Descript ...

  6. POJ1300Door Man(欧拉回路)

                                                               Door Man Time Limit: 1000MS   Memory Limi ...

  7. POJ1300 Door Man —— 欧拉回路(无向图)

    题目链接:http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  8. [欧拉回路] poj 1300 Door Man

    题目链接: http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  9. POJ 1300 Door Man(欧拉通路)

    题目描写叙述: 你是一座大庄园的管家. 庄园有非常多房间,编号为 0.1.2.3..... 你的主人是一个心不在 焉的人,常常沿着走廊任意地把房间的门打开.多年来,你掌握了一个诀窍:沿着一个通道,穿 ...

随机推荐

  1. C#中 多线程执行含有返回值的函数

    C# 中,传统的多线程并不支持多线程执行含有返回结果的函数.虽然可以通过制作外壳类来使得返回结果得以保留,但如果一定时间内函数未执行完,简单的外壳类可能就无法满足需求了. class netHelpe ...

  2. PDA库位商品出库适配算法

    功能描述 代码实现 算法需求 回到顶部 功能描述   在PDA上面,通过扫描订单号,查询该商品的款号,尺码,订单数:同时适配仓库库位商品[SKU],显示该商品所在库位,库存数量,适配数. 因为PDA界 ...

  3. javascript笔记——图片大小检测

    <html> <head> <script type="text/javascript"> var isIE = /msie/i.test(na ...

  4. 快速搭建MongoDB分布式集群

    目录Outline 1. prerequisites 2. steps to follow3. configuring the cluster4. a little test to see 1. Pr ...

  5. flex_播放视频_本地_与_FMS端

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  6. browserify总结

    一.browserify 简介 browserify is a tool for compiling node-flavored commonjs modules for the browser. Y ...

  7. log4j配置只打印指定jar或包的DEBUG信息

    有的时候查问题的时候需要打印第三方jar里面的debug信息,假如全部jar都打印的话日志文件会很大,这个时候可以配置log4j只打印指定jar的debug信息或者包,同时输出到了一个新的文件中. 比 ...

  8. Git 安装与使用(二)

    一.分支管理 在Git里,master是主分支,同时可以创建其他分支,支持各分支合并到主分支上,基本命令如下 1.创建分支 git checkout -b dev       创建dev分支,并切换到 ...

  9. memcached 使用积累

    1.memcahed在windows上的安装 . 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached . 在终端(也即cmd命令界面)下输入 ‘c:\memc ...

  10. 声明(创建) JavaScript 变量

    在 JavaScript 中创建变量通常称为"声明"变量. 我们使用 var 关键词来声明变量: var carname; 变量声明之后,该变量是空的(它没有值). 如需向变量赋值 ...