给定n门n人。门的编号为1到n,人员编号为1到n。每个门只能有2个状态打开和关闭。最初,所有门均处于关闭状态。如果某人更改了所有门的当前状态,即找到状态为打开,然后更改为关闭状态,反之亦然,则找到所有门的最终状态,并向他授权。如果“ j”是“ i”的倍数,则标识为“ i”的人有权更改编号为“ j”的门的状态。
笔记:
–一个人必须更改一次已被授权的所有门的当前状态。
–可能存在这样的情况,即在一个人更改门的状态之前,另一个也被授权使用同一门的人会更改门的状态。
例子 :
Input : 3
Output : open closed closed
说明:由于n = 3,因此有
3门{1、2、3}和
3位ID为{1、2、3}的人
ID = 1的人可以更改门1、2、3的状态
ID = 2的人可以更改2号门的状态
ID = 3的人可以更改3号门的状态
所有门的当前状态:关闭关闭关闭
考虑一系列事件,
- id = 1的人更改门2的状态
所有门的当前状态:关闭打开关闭 - ID = 3的人更改3号门的状态
所有门的当前状态:关闭打开打开 - id = 1的人更改1号门,3号门的状态
所有门的当前状态:打开打开关闭 - id = 2的人更改2号门的状态
所有门的当前状态:打开关闭关闭
另一个例子 :
Input : 5
Output : open closed closed open closed
Note: Sequence of open/closed is displayed in
increasing door number
方法:这是数学和逻辑方法。如果我们正确观察,则发现如果i的因子数为奇数,则编号为i的门的最终状态为打开,而如果i的因子数为偶数,则最终状态为关闭。它不取决于门的状态更改顺序。要查找数字除数的计数是偶数还是奇数,我们可以查看检查除数的计数是偶数还是奇数。
C++
// C++ implementation of
// doors open or closed
#include
using namespace std;
// Function to check whether 'n'
// has even number of factors or not
bool hasEvenNumberOfFactors(int n)
{
int root_n = sqrt(n);
// if 'n' is a perfect square
// it has odd number of factors
if ((root_n*root_n) == n)
return false;
// else 'n' has even
// number of factors
return true;
}
// Function to find and print
// status of each door
void printStatusOfDoors(int n)
{
for (int i=1; i<=n; i++)
{
// If even number of factors
// final status is closed
if (hasEvenNumberOfFactors(i))
cout << "closed" << " ";
// else odd number of factors
// final status is open
else
cout << "open" << " ";
}
}
// Driver program
int main()
{
int n = 5;
printStatusOfDoors(n);
return 0;
}
Java
// java implementation of
// doors open or closed
import java.io.*;
class GFG {
// Function to check whether 'n'
// has even number of factors or not
static boolean hasEvenNumberOfFactors(int n)
{
double root_n = Math.sqrt(n);
// if 'n' is a perfect square
// it has odd number of factors
if ((root_n*root_n) == n)
return false;
// else 'n' has even
// number of factors
return true;
}
// Function to find and print
// status of each door
static void printStatusOfDoors(int n)
{
for (int i = 1 ; i <= n; i++)
{
// If even number of factors
// final status is closed
if (hasEvenNumberOfFactors(i))
System .out.print( "closed" + " ");
// else odd number of factors
// final status is open
else
System.out.print( "open" + " ");
}
}
// Driver program
public static void main (String[] args) {
int n = 5;
printStatusOfDoors(n);
}
}
// This article is contributed by vt_m
Python3
# Python 3 implementation of
# doors open or closed
import math
# Function to check whether
# 'n' has even number of
# factors or not
def hasEvenNumberOfFactors(n):
root_n = math.sqrt(n)
# if 'n' is a perfect square
# it has odd number of factors
if ((root_n * root_n) == n):
return False
# else 'n' has even
# number of factors
return True
# Function to find and print
# status of each door
def printStatusOfDoors(n):
for i in range(1, n + 1):
# If even number of factors
# final status is closed
if (hasEvenNumberOfFactors(i) == True):
print("closed", end =" ")
# else odd number of factors
# final status is open
else:
print("open", end =" ")
# Driver program
n = 5
printStatusOfDoors(n)
# This code is contributed by Smitha Dinesh Semwal
C#
// C# implementation of
// doors open or closed
using System;
class GFG
{
// Function to check whether
// 'n' has even number of
// factors or not
static bool hasEvenNumberOfFactors(int n)
{
double root_n = Math.Sqrt(n);
// if 'n' is a perfect square
// it has odd number of factors
if ((root_n * root_n) == n)
return false;
// else 'n' has even
// number of factors
return true;
}
// Function to find and print
// status of each door
static void printStatusOfDoors(int n)
{
for (int i = 1 ; i <= n; i++)
{
// If even number of factors
// final status is closed
if (hasEvenNumberOfFactors(i))
Console.Write("closed" + " ");
// else odd number of factors
// final status is open
else
Console.Write("open" + " ");
}
}
// Driver Code
static public void Main ()
{
int n = 5;
printStatusOfDoors(n);
}
}
// This Code is contributed by ajit
PHP
Javascript
输出 :
open closed closed open closed