100 doors
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的更多相关文章
- 100 Door Puzzle
问题重述: There are 100 doors in a long hallway. They are all closed. The first time you walk by each do ...
- 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...
- 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 ...
- POJ1300(欧拉回路)
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2139 Accepted: 858 Descripti ...
- POJ 1300.Door Man 欧拉通路
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2596 Accepted: 1046 Descript ...
- POJ1300Door Man(欧拉回路)
Door Man Time Limit: 1000MS Memory Limi ...
- POJ1300 Door Man —— 欧拉回路(无向图)
题目链接:http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- [欧拉回路] poj 1300 Door Man
题目链接: http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- POJ 1300 Door Man(欧拉通路)
题目描写叙述: 你是一座大庄园的管家. 庄园有非常多房间,编号为 0.1.2.3..... 你的主人是一个心不在 焉的人,常常沿着走廊任意地把房间的门打开.多年来,你掌握了一个诀窍:沿着一个通道,穿 ...
随机推荐
- JMS - ConnectionMetaData
A Connection provides a ConnectionMetaData object. This object provides the latest version of JMS su ...
- Linux 命令 - mkdir: 创建目录
命令格式 mkdir [OPTION]... DIRECTORY... 命令参数 -m, --mode=MODE 设置文件的模式,类似于 chmod 命令. -p, --parents 需要时创建指定 ...
- Android PullToRefresh下拉刷新控件的简单使用
PullToRefresh这个开源库早就听说了,不过一直没用过.作为一个经典的的开源库,我觉得还是有必要认识一下. 打开github上的网址:https://github.com/chrisbanes ...
- 使用WDS安装Windows8.1
WDS的部署 安装角色 配置 1. 选择配置服务器 2. 核对是否满足要求 3. 输入远程安装文件夹的路径 4. 选择是否使用自带的DHCP服务器 5. 可以保持默认 6. 完成配置后添加映像文件 7 ...
- MacBook Pro 的照相机在哪?
用于拍照 用于录制视频
- c# DataGridView操作
#region 操作DataGridView /// <summary> /// 初始化DataGridView属性 /// </summary> /// <param ...
- nodejs7.0 试用 async await
nodejs 7.0.0 已经支持使用 --harmony-async-await 选项来开启async 和 await功能. 在我看来,yield 和 async-await 都是在特定范围内实现了 ...
- [Bootstrap]全局样式(四)
按钮 1.基本类.btn {display/padding/margin-bottom/font-size/border-radius/border} 作用于< a:role:button &g ...
- javascript笔记——jikeytang javascript前端群 389875212 精华总结
网址: https://github.com/jsfront // http://www.kancloud.cn/jsfront/month/82796 内容: 前端js github总结, ...
- 在java中HttpServletResponse响应中文出现乱码。
以字符串的形式输出. 1.response.getWriter().write("您好中国hello"); 如果这样输出的话.则浏览器结果为: 2.加上代码 response.se ...