[ACM] POJ 1218 THE DRUNK JAILER (关灯问题)
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/sr19930829/article/details/37727417
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23246 | Accepted: 14641 |
Description
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
Output
Sample Input
2
5
100
Sample Output
2
10
Source
解题思路:
题意为n个监狱,编号1到n,初始均关闭,进行n局游戏,第一局,把全部的监狱都打开,第i(i>=2)局,把编号为 i 的倍数的监狱的状态改变(打开变为关闭或关闭变为打开)。问n局游戏以后,有多少个监狱为打开状态。 用d[]数组来保存监狱的状态。模拟n局游戏就能够了。
代码:
#include <iostream>
#include <string.h>
#include <stack>
#include <iomanip>
#include <cmath>
using namespace std;
bool d[110];
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int cnt=0;
memset(d,0,sizeof(d));
for(int i=2;i<=n;i++)
{
for(int j=i;j<=n;j+=i)
d[j]=1-d[j];//状态改变
}
for(int i=1;i<=n;i++)
if(d[i]==0)
cnt++;
cout<<cnt<<endl;
}
return 0;
}
[ACM] POJ 1218 THE DRUNK JAILER (关灯问题)的更多相关文章
- poj 1218 THE DRUNK JAILER【水题】
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25124 Accepted: 1576 ...
- poj 1218 THE DRUNK JAILER
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23358 Accepted: 1472 ...
- POJ 1218 THE DRUNK JAILER(类开灯问题,完全平方数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2188 题目大意:n为5-100之间的一个数,代表有多少间牢房,刚开始所有房间打开,第一轮2的倍数的房间 ...
- THE DRUNK JAILER 分类: POJ 2015-06-10 14:50 13人阅读 评论(0) 收藏
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24918 Accepted: 1563 ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
- HDU 1337 && POJ 1218&& zju 1350 方法总结
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zj ...
- ACM: POJ 1401 Factorial-数论专题-水题
POJ 1401 Factorial Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- [ACM] POJ 2151 Check the difficulty of problems (概率+DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4748 ...
- THE DRUNK JAILER
http://poj.org/problem?id=1218 题意:门的状态有两种开或关,初始化为开,每次进行状态转换,第一次把门号是1的倍数的门状态转换,第二次把门号是2的倍数的门状态转换,.... ...
随机推荐
- C# 笔记 获取程序当前目录
在C#中,我们有以下几种方式获取程序当前目录: Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssem ...
- putchar(".:-=+*#%@"[(int)(d * 5.0f)])
前两天在玩知乎时候见到有个用C语言画心的小代码感觉还是蛮好玩的,不过,里面有行代码看了好久才懂: putchar(".:-=+*#%@"[(int)(d * 5.0f)]); ,先 ...
- xampp环境下,配置Zend Studio调试php(XDebug) 转摘:http://www.cnblogs.com/tuyithief/archive/2011/06/02/2068431.html
先说一下文件版本,xampp 1.7.4,php 5.3.5. 走了很多弯路,截止目前,ZendDebugger在php 5.3.x下,只有nts版本,既non Thread Safety(具体什么意 ...
- 四、spring的JDBC模板和事务管理
Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...
- scroll兼容性
document.html=>document.documentElement function scroll() { if(window.pageYOffset != null) // ie9 ...
- go语言从例子开始之Example12.func函数
函数 是 Go 的中心.我们将通过一些不同的例子来进行学习. Example: package main import "fmt" //定义一个函数使用关键字func //函数名名 ...
- exp ORA-01455: converting column overflows integer datatype
EXP-00008: ORACLE error 1455 encounteredORA-01455: converting column overflows integer datatype add ...
- 扩展阿里巴巴Java开发规约插件(转)
转自:https://blog.csdn.net/u014513883/article/details/79186893 1.前言 工作中难免会遇到维护别人代码的情况,那么首先就得看懂别人写的代码.如 ...
- GPIO软件模拟IIC时序
一.MPU6050中的IIC时序 1.1 START和STOP SDA和SCL在高电平时,SDA拉低表示START.SCL拉低,表示可以传输数据. SDA和SCL在低电平时,SDA拉高表示STOP. ...
- js调用ios和安卓方法
安卓: window.AndroidWebView.方法名(参数); window.AndroidWebView.productDetail(1989); ios: function isbrowse ...