POJ 1218 THE DRUNK JAILER

开关问题,有n个灯,分别由n个开关控制,拨一下开关则可以改变灯的状态(开->关 关->开)。初始状态灯都是关着的,先把每个开关都拨一下,然后拨一下2的倍数的开关,接着3的倍数,直到n的倍数,问最后有多少灯是开着的。

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int t, a[110], k;
        memset(a, 0, sizeof(a));
        cin>>t;
        for(int i=1; i<=t; i++)
        {
            for(int j=1; i*j<=t; j++)
                a[i*j]=!a[i*j];
        }
        k=0;
        for(int i=1; i<=t; i++)
        {
            if(a[i]==1)
                k++;
        }
        cout<<k<<endl;
    }
    return 0;
}
时间: 2024-12-31 09:00:50

POJ 1218 THE DRUNK JAILER的相关文章

[ACM] POJ 1218 THE DRUNK JAILER (关灯问题)

THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23246   Accepted: 14641 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

开灯问题 —— POJ 1218 THE DRUNK JAILER

对应POJ 题目:点击打开链接 THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24831   Accepted: 15569 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 eac

poj 1218 THE DRUNK JAILER【水题】

THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25124   Accepted: 15767 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

HDU 1218 THE DRUNK JAILER【类开灯问题】

THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27268   Accepted: 16933 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

POJ1218 THE DRUNK JAILER

问题链接:POJ1218 THE DRUNK JAILER. 题意简述:输入n,n为5-100间的一个数,代表有多少间牢房.刚开始所有房间打开,第1轮2的倍数的房间门翻转(打开的关上,关上的打开),第2轮3的倍数,第3轮4的倍数,......,第n-1轮n的倍数.求最后有几间牢房门是打开的. 问题分析:使用模拟法,模拟这个过程即可.好在计算规模不算大. AC的C语言程序如下: /* POJ1218 THE DRUNK JAILER */ #include <stdio.h> #include

1218THE DRUNK JAILER

THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25010   Accepted: 15692 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

hdu 1337 The Drunk Jailer

http://acm.hdu.edu.cn/showproblem.php?pid=1337 1 #include <cstdio> 2 #include <cstring> 3 #define maxn 200 4 using namespace std; 5 int main() 6 { 7 int t; 8 int a[maxn]; 9 scanf("%d",&t); 10 int n; 11 while(t--) 12 { 13 scanf(&q

HDU 1337 &amp;&amp; POJ 1218&amp;&amp; zju 1350 方法总结

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1350浙江 原本以为小小的一道水题不值得去总结,但是发现却有好多方法,好神奇啊 方法一:数据挺小的模拟就好 #include<iostream> #include<algorithm> #

POJ 1218

题目描述看着就乐了,死板得只按着题意来写了ps: tequi是escape的方言版.. #include <iostream> using namespace std; int main() { int line,i,k; int ln[250]; //This is the number of lines that follow. cin>>line; //input for(int j=0;j<line;j++){ cin>>ln[j];//用空格分开 } /