zoukankan      html  css  js  c++  java
  • 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 }
    
    
    




  • 相关阅读:
    使用delphi 开发多层应用(十)安全访问服务器
    使用delphi 开发多层应用(十三)使用Basic4android 直接访问kbmMW server
    使用delphi 开发多层应用(十一)使用kbmMW 开发webserver
    basic4android 开发教程翻译(八)使用ListView
    使用delphi 连接国产数据库:达梦
    kbmMW 4.01.00 Beta 1发布了
    解决 ie 下 javascript 设置 table.tBodies.innerHTML 无法设置的问题
    用vs2010 把 vcf 转成 csv,再转入Nokia
    GAC 与 引用 程序集路径
    sqlserver 函数 之 进制转换
  • 原文地址:https://www.cnblogs.com/CocoonFan/p/2939602.html
Copyright © 2011-2022 走看看