题目描述

There are n lockers in a hallway numbered sequentially from 1 to n. Initially, all the locker doors are closed. You make n passes by the lockers, each time starting with locker #1. On the ith pass, i = 1, 2, …, n, you toggle the door of every ith locker: if the door is closed, you open it, if it is open, you close it. For example, after the first pass every door is open; on the second pass you only toggle the even-numbered lockers (#2, #4, …) so that after the second pass the even doors are closed and the odd ones are opened; the third time through you close the door of locker #3 (opened from the first pass), open the door of locker #6 (closed from the second pass), and so on. After the last pass, which locker doors are open and which are closed? How many of them are open? Your task is write a program to output How many doors are open after the last pass? Assumptions all doors are closed at first.

输入

a positive numbers n, total doors. n<=100000

输出

a positive numbers ,the total of doors opened after the last pass.

样例输入

10

样例输出

3

代码

#include<iostream>
#include<cmath>
using namespace std;
int main(){int n;cin>>n;int m =sqrt(n);cout<<m<<endl;return 0;
}

超级简单有没有?是的,就这样。为什么呢?下面有详细解说。

思路

大多数情况下,每个数的因子是成对出现的,例如数15的因子有1和15,以及3和5,数12的因子有1和12,2和6,3和4,所以有偶数个因子。只有当这个数是完全平方数时,例如1,4,9,16,25,36等等,它的因子除成对出现的以外,还有它的整数平方根作为单独的因子,这些完全平方数的因子数为奇数。
最后一次经过后,只有编号为1,4,9,16,25,36,49,81…的门会开着,其余编号的门都关着。打开的门总数为(sqrt(n))

算法——Locker doors相关推荐

  1. 【算法----->Locker Doors】

    LockerDoors 题目 输入 输出 代码实现(Java) 例子 题目 There are n lockers in a hallway numbered sequentially from 1 ...

  2. SWUSTOJ #480 Locker doors

    SWUSTOJ #480 Locker doors 题目 输入 输出 样例输入 样例输出 源代码 题目 There are n lockers in a hallway numbered sequen ...

  3. There are n lockers in a hallway numbered sequentially from 1 to n. Initially, all the locker doors

    package xuexi; /*  * n把锁问题  */ import java.util.ArrayList; import java.util.Arrays; import java.util ...

  4. 分布式ID自增算法 Snowflake

    近在尝试EF的多数据库移植,但是原始项目中主键用的Sqlserver的GUID.MySQL没法移植了. 其实发现GUID也没法保证数据的递增性,又不太想使用int递增主键,就开始探索别的ID形式. 后 ...

  5. [Qt] 迷宫随机生成和自动寻路算法、布局管理器、动态效果、界面切换、播放音频【迷宫、魔塔、超级玛丽】 -C++课程设计:Qt实现的迷宫与地牢游戏

    基于QT的迷宫与地牢小游戏开发 首先贴出链接: 完整Qt源码:点击进入https://download.csdn.net/download/qq_43365825/11852112 发布可执行程序:点 ...

  6. 关于计算几何一些算法

    http://www.cppblog.com/RyanWang/archive/2009/02/09/73275.aspx 其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中. ...

  7. 油漆算法问题_不同类型的油漆(以及何时使用它们)

    油漆算法问题 If you're about to attack the walls in your house with a fresh new coat of paint, all the dif ...

  8. python nlp文本摘要实现_用TextRank算法实现自动文本摘要

    [51CTO.com快译]1. 引言 文本摘要是自然语言处理(NLP)领域中的应用之一,它必将对我们的生活产生巨大影响.随着数字媒体和出 版业的不断发展,谁还有时间浏览整篇文章/文档/书籍来决定它们是 ...

  9. 使用TextRank算法进行文本摘要提取(python代码)

    文本摘要是自然语言处理(NLP)的一种应用,随着人工智能的发展文本提取必将对我们的生活产生巨大的影响.随着网络的发展我们处在一个信息爆炸的时代,通读每天更新的海量文章/文档/书籍会占用我们大量的时间, ...

  10. 基于TextRank算法的单领域多文本摘要(英文摘要)

    文章目录 基于TextRank算法的单领域多文本摘要 一.实现步骤 二.语料 tennis_articles_v4.csv 三.实现代码 四.实现效果 基于TextRank算法的单领域多文本摘要 一种 ...

最新文章

  1. wkwebView基本使用方法
  2. 解决toad中number类型小数位数过长按科学计数法显示的问题
  3. 解决从本地文件系统上传到HDFS时的权限问题
  4. Oracle 数据表误删恢复 Flashback
  5. linux 进程 setuid,Linux SetUID(SUID)文件特殊权限用法详解
  6. C++后台开发需要什么?
  7. 在django中使用celery
  8. JSON serializing and deserializing using JSON.NET
  9. 刷bios工具_微星主板怎么更新bios版本?微星主板更新BIOS版本图文教程
  10. javascript 库_您应该在2020年尝试的10个很棒JavaScript库
  11. linux 错误信息 输出到管道,linux grep通过管道输出到head出现write error: Broken pipe错误...
  12. 借贷平台Liquity昨日共有超300个Troves被清算
  13. [BZOJ]3727: PA2014 Final Zadanie
  14. 自动化测试 之连连看外挂项目实战
  15. 漫游费概念模糊,运营商自食其果
  16. 前端开发-css续3
  17. 2021周记11:慢慢自律和追剧
  18. Android:Handler中的Idle Handler
  19. 关于AI测试与评价的讨论
  20. 计算机原理与应用作业,计算机原理与应用(杨刚)-中国大学mooc-题库零氪

热门文章

  1. docker容器内启动mysql服务,报错:New main PID 99 does not belong to service, and PID file is not owned by root.
  2. 我见过最“骚”的代码注释!神兽版都来了
  3. win10 下装win7
  4. 【JAVA】RequestResponse
  5. Pale Moon 苍月浏览器 24.0.1 发布
  6. tp5.1发送公众号模板、订阅消息
  7. EdgeBox_EHub_tx1_tx2_E100 系统网络调试链接说明
  8. 使用viewer实现图片预览
  9. html 大转盘游戏,HTML5 Canvas大转盘抽奖活动页面代码
  10. QR扫码综合示例教程(六)Qt6.2.1(widget)取出视频帧 取景器帧