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. extjs的调试方法

    1.使用extjs自带的测试工具 第一步:在ExtJS下载的资源包中,找到debug.js,将JS文件导入实际要运行的HTML或者JSP页面上 第二步:在有关JS文件代码中嵌入Ext.log('自定义 ...

  2. codevs4373 窗口==poj2823 Sliding Window

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53676   Accepted: 15399 ...

  3. css3 过渡记

    CSS3 过渡 CSS3的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击,获得焦点,被点击或对元素任何改变中触发,并平滑地以动画效果改变CSS的属性值. t ...

  4. MyBatis(3.2.3) - Passing multiple input parameters

    MyBatis's mapped statements have the parameterType attribute to specify the type of input parameter. ...

  5. T-SQL 使用链接库向mysql导数据遇到的奇葩事件一

    mysql表结构有 主键 非自增 text longtext类型字段多个 步骤 1.在T-SQL 临时表中处理好所有需要的字段 2.执行openquery语句 字段顺序完全按照mysql字段顺序插入 ...

  6. 个人常用jq方法复习

    $("#elem").on({ mouseover:function(){}, mouseout:function(){}, }); $(ele).closest("di ...

  7. android结束进程、退出application的方法

    1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...

  8. JSPatch 遇上swift

    swift使用JSPatch要点: 继承自NSObject的Swift类,其继承自父类的方法具有动态性,其他自定义方法.属性需要加dynamic修饰才可以获得动态性(public属性除外) 例子可参看 ...

  9. (转)Spark安装与学习

    摘要:Spark是继Hadoop之后的新一代大数据分布式处理框架,由UC Berkeley的Matei Zaharia主导开发.我只能说是神一样的人物造就的神器,详情请猛击http://www.spa ...

  10. (转) 读取xml文件转成List<T>对象的两种方法

    读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法,加上自己知道的另一种实现方法. 就以一个简单的xml做例子. xml格 ...