【算法----->Locker Doors】
LockerDoors
- 题目
- 输入
- 输出
- 代码实现(Java)
- 例子
题目
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.
代码实现(Java)
public class LockerDooor {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);}
}
例子
输入:4
第一次:1 1 1 1
第二次:1 0 1 0
第三次:1 0 0 0
第四次:1 0 0 1
此时输出:2
输入:9
最开始: 0 0 0 0 0 0 0 0 0
第一次:1 1 1 1 1 1 1 1 1
第二次:1 0 1 0 1 0 1 0 1
第三次:1 0 0 0 1 1 1 0 0
第四次:1 0 0 1 1 1 1 1 0
第五次:1 0 0 1 0 1 1 1 0
第六次:1 0 0 1 0 0 1 1 0
第七次:1 0 0 1 0 0 0 1 0
第八次:1 0 0 1 0 0 0 0 0
第九次:1 0 0 1 0 0 0 0 1
输出:3
总结:找规律得出输出值=输入值的平方根整数(不太清楚的可以多写点输入的值)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
