题目描述
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

题目分析:
最开始 0 0 0 0 0 0 0 0 0 0
第一次 1 1 1 1 1 1 1 1 1 1
第二次 1 0 1 0 1 0 1 0 1 0
第三次 1 0 0 0 1 1 1 0 0 0
第四次 1 0 0 1 1 1 1 1 0 0
第五次 1 0 0 1 0 1 1 1 0 1
第六次 1 0 0 1 0 0 1 1 0 1
第七次 1 0 0 1 0 0 0 1 0 1
第八次 1 0 0 1 0 0 0 0 0 1
第九次 1 0 0 1 0 0 0 0 1 1
第十次 1 0 0 1 0 0 0 0 1 0
规律
从这个规律我们可以看出,每次关闭第i次经过的i的整数倍的门,根据最后的结果可以看出来,这就是求n内的平方数有多少个(例:n=10,平方数为1,4,9,三个)

import java.util.Scanner;public class LockerDoors {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int sqrt = (int) Math.sqrt(n);System.out.println(sqrt);}
}

如果没有找到规律就直接暴力求解

import java.util.Scanner;public class LockerDoors1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();Boolean[] arr = new Boolean[n];int N = arr.length;for (int i = 0; i <N ; i++) {arr[i] = true;}for (int i = 2; i <= N; i++) {for (int j = i-1; j<N ; j+=i) {arr[j] = !arr[j];}}int count = 0;for (int i = 0; i < N; i++) {if (arr[i] == true){count++;}}System.out.println(count);}
}

SWUST OJ 480: Locker doors相关推荐

  1. SWUSTOJ #480 Locker doors

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

  2. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Descr ...

  3. SWUST OJ 954单链表的链接

    swust oj 954 题目描述 建立长度为n的单链表A和长度为m的单链表B.编程实现将B表链接在A表的尾端,形成一个单链表A.数据类型指定为字符型. 输入 输出 样例输入 样例输出 源代码 #in ...

  4. swust oj#160促销计算

    SWUST OJ#160 题目描述 某百货公司为了促销,采用购物打折的优惠方法,每位顾客一次购物:在1000元以上者,按9.5折优惠:在2000以上者,按9折优惠:在3000以上者,按8.5折优惠:在 ...

  5. SWUST OJ 1168 喝可乐

    swust oj 1168 题目描述 小明十分喜欢喝可乐,有一次店家搞促销,用三个可乐瓶盖便可换一瓶新可乐.现在告诉你小明身上的钱和 每瓶可乐的单价,问你小明最多可以喝多少瓶可乐?(不能向老板借瓶盖) ...

  6. swust oj代码+解析_1165,0284,0074,0042,1171,0026,0189,0078,0046,0077,0209,0129

    swust oj 1165,0284(int a[n]\数字根),0074,0042,1171(矩阵相乘 输出对齐),0026/0189,0078(计算生日是星期几),0046,0077(计算员工周工 ...

  7. SWUST OJ#281逃跑的蠕虫

    swust oj 281 题目描述 装在瓶子(瓶子高度为h)的蠕虫都想从瓶子底部向瓶口处爬出去.它每分钟向上爬行u厘米,之后会休息一分钟,这一分钟它会向下滑行d厘米,当蠕虫到了瓶口或者超出瓶口后便出了 ...

  8. SWUST OJ#978 #979 #980 二叉树的遍历

    目录 深度优先遍历 输出利用先序遍历创建的二叉树的前序遍历序列 思路 代码 #978 输出利用先序遍历创建的二叉树的中序遍历序列 题目 思路 代码 #979 输出利用先序遍历创建的二叉树的后序遍历序列 ...

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

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

  10. SWUST OJ 1159 吃披萨

    swust oj 1159 题目描述 小明楼下新开了两家披萨店,价格都一样,不同的是A家披萨店的披萨是圆形,B家披萨店的披萨是三角形.为了知道 哪家披萨店的披萨面积更大一些,于是就找到你咯,你来帮帮他 ...

最新文章

  1. 二级c语言函数调用题,2013年计算机二级C语言函数调用考点归纳
  2. 顺序图组合片段类型及属性
  3. 再谈谈ADO.NET Data Service
  4. LDAPimplementation
  5. ROS入门-9.订阅者Subscriber的编程实现
  6. python输出文本文件_Python进阶02 文本文件的输入输出
  7. 浙大学霸Facebook总部跳楼:永远不要把公司当成“家”
  8. JavaScript-Tool:CKFinder
  9. QT造类器(操作简单)
  10. twitter最多关注者_Twitter的10个最具创意的用途
  11. Qcom平台,dump解析环境配置
  12. 栈(Stack)——后进先出(LIFO)的数据结构(Data Structures)
  13. PDF怎么转图片?快把这些方法收好
  14. 移动应用程序设计基础——期末考核——登录界面与简单日记本的综合实践
  15. 网页打开速度很慢,怎么解决?
  16. python小乌龟绘制迷宫_用turtle不断的画回字迷宫
  17. 实时折线图php mysql 源码_超级漂亮网址导航源码,自助链源码(PHP+MYSQL完整版)...
  18. OD:修改 navicat 试用期
  19. 幽默笑话,哥们误会了,木子家原创
  20. 黑群晖(DSM7)使用docker挂载zerotier one实现内网穿透

热门文章

  1. 在“动物杂交:新视野”中快速赚钱的9种方法
  2. 2021/10/15 考试总结
  3. Android:执行exec app_process启动jar失败原因
  4. 零信任兴起:从理念到实践
  5. 这也能卖?拉美电商平台Mercado Libre上的10种奇葩产品
  6. 吉利车机安装第三方app教程,支持缤瑞、缤越、博越、博瑞ge、星越等
  7. 入门3D建模学习教程,让你最快从小白到建模大师!
  8. Java如何处理参数中带特殊符号的请求?
  9. 还在手写记单词?使用Python开发练习英语单词,助你逆袭单词记忆王!
  10. 在GraphPad Prism Mac中处理多份文件