[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的倍数的门状态转换,.... ...
随机推荐
- golang的数据类型之字符串类型
基本案例: [root@node3 shangxuetang]# cat string.go package main import "fmt" func main() { //s ...
- Vue.config.silent = true
Vue.config vue的全局配置文件 silent默认值是false Vue.config.silent = true 取消Vue所有的日志和警告
- 页面跳转到Area区域连接
@Html.ActionLink("主页", "Index", new { controller = "Test", Action = &q ...
- HttpClient 之 发送Https请求
HttpClient包是一个优秀的Http请求的开源jar. 本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求. 但是由于项目的需要快速的实现,以下代码还可能会有点过 ...
- Resultset获取行数和列数
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Statement stmt = con.create ...
- Vue中组件间传值常用的几种方式
版本说明: vue-cli:3.0 一.父子组件间传值 1.props/$emit -父组件==>>子组件: 子组件中通过定义props接收父组件中通过v-bind绑定的数据 父组件代码 ...
- 【知识强化】第六章 查找 6.3 B树和B+树
本节课我们来学习本章的第一个难点,就是B树.那么B树它其实是一种数据结构,我们设计出这种数据结构就是为了提高我们的查找效率的,提高我们在磁盘上的查找效率.那么什么是B树呢?了解B树之前,我们先来回忆一 ...
- 2019HDU多校训练第三场 Planting Trees 暴力 + 单调队列优化
题意:有一个n * n的网格,每个网格中间有一颗树,你知道每棵树的高,你可以选择一个矩形区域把里面的树都围起来,但是矩形区域里面任意两棵树的高度差的绝对值不超过m,问这个矩形的最大面积是多少? 思路: ...
- window杀死端口
获取端口的pid:netstat -aon|findstr "8382" 杀死pid : taskkill /pid [] -t -f
- Guarded Suspension模式简单实现
Guarded Suspension 意为保护暂停,假设服务器很短时间内承受大量的客户端请求,客户端请求的数量超过服务器本身的即时处理能力,而服务器又不能丢弃任何一个客户端请求,此时可以让客户端的请求 ...