POJ 1218 THE DRUNK JAILER(醉汉看守)

THE DRUNK JAILER
 
Time Limit: 1000MS Memory Limit: 10000K

Description

    A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked. One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey,and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell (cells 2, 4, 6, ?). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, ?). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out. Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.
Given the number of cells, determine how many prisoners escape jail.

Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.

Output

For each line, you must print out the number of prisoners that escape when the prison has n cells.

Sample Input

2
5
100

Sample Output

2
10

【题目大意】
  一个监狱看守员喝醉了酒,于是把监狱每扇门都打开(假设有n扇门);然后再从1号门开始,隔一扇关一个门(把2的倍数的门关掉);
接着再从
1号门开始,隔2扇操作一个门(操作3的倍数的门,原来是开的关掉,关着的则打开)。这样一直操作到n的倍数,问最后有多少扇门是打开的。
【题目分析】                                                                             
  · 用一个数组存放门的状态(开或关)  
  · 用两个循环来改变门的状态
  
  · 输出结果
【代码描述】
 1 /*============================================================================*\
 2 *
 3 * POJ 1218 THE DRUNK JAILER(醉汉看守)
 4 * @author CocoonFan
 5 * @date 3/1/2013
 6 \*============================================================================*/
 7 
 8 #include <iostream>
 9 #include <cstring>
10 
11 int main()
12 {
13     char lock[200];//用char节省空间
14     int testCase, n, ans;
15     std::cin >> testCase;
16     while(testCase--){
17         ans = 0;
18         memset(lock, 0, sizeof(lock));//初始化memset()在 cstring 中
19         std::cin >> n;
20 
21         for(int i = 1; i <= n; ++i)   //没有用lock[0]
22             for(int j = i; j <= n; j += i)
23                 lock[j] = 1-lock[j];
24 
25         for(int i = 1; i <= n; ++i)
26             if(lock[i])
27                 ++ans;
28 
29         std::cout << ans << std::endl;
30 
31     }
32 
33     return 0;
34 }
【另一种描述】
在转换状态的同时记录 ans 的值
 1 /*============================================================================*\
 2 * POJ 1218 THE DRUNK JAILER (醉汉看守)
 3 * 2013/3/1
 4 * CocoonFan
 5 \*============================================================================*/
 6 
 7 #include<iostream>
 8 #include<cstring>
 9 
10 const int MAX_LEN = 200;
11 

12 int main()
13 {
14     int t;
15     std::cin >> t;
16     int a[MAX_LEN];
17     while(t--){
18         memset(a,0,sizeof(a));
19         int n;
20         std::cin >> n;
21         int ans = 0;
22 
23         for(int i = 1; i <= n; ++i){
24             for(int j = i; j <= n; j += i ){
25                 if(a[j]){
26                     a[j] = 0;
27                     --ans;
28                 } else {
29                     a[j] = 1;
30                     ++ans;
31                 }
32             }
33         }
34 
35         std::cout << ans << std::endl;
36     }
37 
38 
39     return 0;
40 }
【进一步分析】
  本题有意思的在后面,我们先把1~100之内的结果列表看看。

  看到规律了吧,结果就是牢房数的平方根!!!

  于是我们很快得出了简短高效的代码:

 1 #include <iostream>
 2 #include <cmath>
 3 
 4 int main()
 5 {
 6     int n, a, k;
 7     std::cin >> n;
 8     while(n--){
 9         std::cin >> k;
10         a = sqrt(k);
11         std::cout << a << std::endl;
12     }
13     return 0;
14 }

 

 
 

 





转载于:https://www.cnblogs.com/CocoonFan/archive/2013/03/02/2939602.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值