11,808
社区成员




某监狱有一个由n个牢房组成的大厅,每个牢房紧挨着。每个牢房里都有一个囚犯,每个牢房都是锁着的。
一天晚上,狱卒感到无聊,决定玩一个游戏。在第一轮,他喝了一杯威士忌,然后跑下大厅,打开每个牢房的锁。在第二轮比赛中,他喝了一杯威士忌,然后跑下大厅,锁上每隔一个的牢房的锁(牢房2、4、6....)。在第三轮比赛中,他喝了一杯威士忌,然后跑下大厅。他每隔三个牢房(第3、6、9号牢房)就去一次。如果牢房被锁上了,他就把它打开;如果牢房门打开了,他就锁上牢房。他重复n轮,喝最后一杯,然后昏倒。
一些囚犯(可能为零号)意识到他们的牢房被解锁且狱卒丧失了行动能力。他们就可以立即逃跑。现在根据牢房数量,确定有多少囚犯越狱。
输入描述:
第一行输入包含一个正整数t。表示有t行数据,下面每一行都包含一个介于5和100之间(含5和100)的整数,即轮数n
输出描述:
对于每一行,必须打印出监狱有n个牢房时越狱的囚犯人数
输入样例:
2
5
100
输出样例:
2
10
public static void main(String[] args) {
int n = 5; // 可以替换为任意牢房数量
int escapedPrisoners = prisonBreak(n);
System.out.println("有 " + escapedPrisoners + " 个囚犯越狱。");
}
public static int prisonBreak(int n) {
// 牢房状态数组,true表示牢房被锁上
boolean[] cells = new boolean[n];
// 进行n轮比赛
for (int i = 1; i <= n; i++) {
// 每隔i个牢房进行一次操作
for (int j = i - 1; j < n; j += i) {
// 切换牢房状态
cells[j] = !cells[j];
}
}
// 统计被解锁的牢房数量
int count = 0;
for (boolean cell : cells) {
if (!cell) {
count++;
}
}
return count;
}
def main(a,n):
a_list = []
for i_a in range(1,n+1):
for i in range(1,a+1):
if i*i_a >a:
continue
if i*i_a in a_list:
a_list.remove(i*i_a)
else:
a_list.append(i*i_a)
print(a_list,'\n',len(a_list))
if __name__ == '__main__':
a = int(input('请输入牢房数'))
n = int(input('请输入喝几轮'))
main(a,n)
#include <iostream>
#include <vector>
int calculateEscapedPrisoners(int n) {
// 每隔一轮牢房数会被解锁,即1号、2号、3号、...、n号
// 若n为偶数,则每隔两轮,牢房数会被重新锁住,如4号、8号、12号...
// 若n为奇数,则每隔三轮,牢房数会被重新锁住,如6号、9号、12号...
// 因此,奇数轮数对应的牢房数会保持解锁状态
if (n % 2 == 1) {
return n / 2 + 1;
}
return n / 2;
}
int main() {
int t;
std::cin >> t;
std::vector<int> results;
for (int i = 0; i < t; i++) {
int n;
std::cin >> n;
int escaped = calculateEscapedPrisoners(n);
results.push_back(escaped);
}
for (int i : results) {
std::cout << i << std::endl;
}
return 0;
}
import java.util.Scanner;
public class Txia0718 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入牢房数:");
int n = sc.nextInt();
System.out.print("请输入喝酒杯数:");
int m = sc.nextInt();
System.out.println("---------------------------");
boolean[] a = new boolean[n];
for (int i = 1; i <= m; i++) {
for (int j = 0; j < a.length; j++) {
if ((j+1)%i==0) a[j] = !a[j];
}
}
System.out.print("最终打开牢门的牢房编号为:");
for (int i = 0; i < a.length; i++) {
if (a[i]) {
System.out.print((i+1) + " ");
}
}
}
}
/**
* 计算越狱人数
* @param n n代表牢房的个数,也代表轮数
* @return
*/
public static int func(int n){
//逃跑的人数
int escape = 0;
int number = n;
int max = n;
for (int i = 1; i <= n; i++) {
// i表示牢房的号码
// s:次数
int s = 0;
for (int j = 1; j <= n; j++) {
//用i取余j 若余数等于0则s+1
if (i % j == 0) {
s++;
}
}
//s%2=1则表示这个牢房最后是打开的,越狱成功
if (s % 2 != 0) {
escape++;
}
}
return escape;
}
我没读懂题好像
#include<stdio.h>
int coefficient=2;
int sum_run;
int num_room,num_drink;
void indat(){
//输入房间数和轮数
scanf("%d",&num_room);
scanf("%d",&num_drink);
}
int main(){
indat();
int room[num_room]={0};//第一轮全都打开
for(int i=0;i<num_drink;i++){
for(int j=1;j<num_room/coefficient;j++){
room[coefficient*j-1]=1-room[coefficient*j-1];
}
coefficient++;
}
for(int k=0;k<num_room;k++){
sum_run+=room[k];
}
printf("%d\n",sum_run);//跑的人数
}
def escape_prisoners(n):
# 计算逃脱的囚犯数量
escaped = 0
# 检查1到n的每个数字
for i in range(1, n+1):
# 如果i是完全平方数,那么这个牢房是打开的,囚犯逃脱
if i**0.5 == int(i**0.5):
escaped += 1
return escaped
n = int(input("请输入牢房数量: "))
print(f"{escape_prisoners(n)} 名囚犯逃脱了。")
#include <stdio.h>
int main() {
int i, j, m = 0, hang, cishu, a[101] = {0};
scanf("%d", &hang);
while (hang > 0) {
hang--;
scanf("%d", &cishu);
for (i = 1; i <= cishu; i++) {
for (j = 1; j <= cishu; j++) {
if (j % i == 0) {
a[j] = 1 - a[j];
}
}
}
for (i = 1; i <= cishu; i++) {
m += a[i];
}
for (i = 1; i <= cishu; i++) {
a[i] = 0;
}
printf("%d\n", m);
m = 0;
}
return 0;
}
#include <stdio.h>
int main()
{
int i, j, m = 0, hang, cishu, a[101] = {0};
scanf("%d", &hang);
while (hang > 0)
{
hang--;
scanf("%d", &cishu);
for (i = 1; i <= cishu; i++)
{
for (j = 1; j <= cishu; j++)
{
if (j % i == 0)
a[j] = 1 - a[j];
}
}
for (i = 1; i <= cishu; i++)
m += a[i];
for (i = 1; i <= cishu; i++)
a[i] = 0;
printf("%d\n", m);
m = 0;
}
return 0;
}
t = int(input('输入包含一个正整数t(表示有t行数据): '))
result = []
for item in range(t):
number = int(input('请输入轮数n,介于5和100之间(含5和100)的整数:'))
# 默认所有牢房都是关闭状态
current_status = { k:'closed' for k in range(1,number+1) }
# 游戏进行的次数和牢房数量相同
lun = number
for l in range(1,lun+1):
for s in range(l,number+1,l):
if current_status.get(s) == 'closed':
current_status[s] = 'open'
else:
current_status[s] = 'closed'
#把状态是open的保存下来
result.append( len( [ i for i in current_status.values() if i == 'open' ] ) )
for i in result:
print('牢房中越狱的囚犯人数:',i)
def jail_break(rooms,turns):
all_r = [0] * (rooms + 1)#房间数量,[0]不用,初始值0为第二次,表示门全开了,到时候,改变次数为单数,就是门关闭状态
all_r[0] = "changeTimes"
for i in range(2, turns + 1):
for r in range(i, rooms+1 ,i):
all_r[r] += 1
for ix in range(1,rooms + 1):
all_r[ix] %= 2
all_r[0] = "Man"
print("越狱人数:%d "%(rooms - sum(all_r[1:rooms+1])))
#=================================================================
jail_break(10,5)
print("rooms?")
rooms = int(input())
print("turns?")
turns = int(input())
all_r = [0] * (rooms + 1)#房间数量,[0]不用,初始值0为第二次,表示门全开了,到时候,改变次数为单数,就是门关闭状态
all_r[0] = "changeTimes"
for i in range(2, turns + 1):
for r in range(i, rooms+1 ,i):
all_r[r] += 1
for ix in range(1,rooms + 1):
all_r[ix] %= 2
all_r[0] = "Man"
print("越狱人数:%d "%(rooms - sum(all_r[1:rooms+1])))
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
bool lock[n + 1] = {false};
for (int i = 1; i <= n; i++) {
int cnt = 0;
if (i % 2 == 1) {
lock[i] = true;
} else {
cnt++;
}
if (i % 3 == 0) {
lock[i] = !lock[i];
} else {
cnt++;
}
if (cnt == 2) {
lock[i] = !lock[i];
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!lock[i]) {
ans++;
}
}
printf("%d\n", ans);
}
return 0;
}
题目描述
有一个由n个牢房组成的大厅,每个牢房紧挨着。每个牢房里都有一个囚犯,每个牢房都是锁着的。
一天晚上,狱卒感到无聊,决定玩一个游戏。在第一轮,他喝了一杯威士忌,然后跑下大厅,打开每个牢房的锁。在第二轮比赛中,他喝了一杯威士忌,然后跑下大厅,锁上每隔一个的牢房的锁(牢房2、4、6....)。在第三轮比赛中,他喝了一杯威士忌,然后跑下大厅。他每隔三个牢房(第3、6、9号牢房)就去一次。如果牢房被锁上了,他就把它打开;如果牢房门打开了,他就锁上牢房。他重复n轮,喝最后一杯,然后昏倒。
一些囚犯(可能为零号)意识到他们的牢房被解锁且狱卒丧失了行动能力。他们就可以立即逃跑。现在根据牢房数量,确定有多少囚犯越狱。
输入格式
第一行输入包含一个正整数t,表示有t行数据,下面每一行都包含一个介于5和100之间(含5和100)的整数,即轮数n。
输出格式
对于每一行,必须打印出监狱有n个牢房时越狱的囚犯人数。
输入样例
2
5
100
输出样例
2
10
算法
(数学公式)
�
(
1
)
O(1)
在第奇数轮(1, 3, 5, ...)牢房被打开,囚犯能够逃跑;在第偶数轮(2, 4, 6, ...)牢房被锁上,囚犯无法逃跑;因此,在n轮下,能够逃跑的囚犯数量为n的平方根向下取整。
时间复杂度
计算能够逃跑的囚犯数量的时间复杂度为
�
(
1
)
O(1)。
空间复杂度
只使用了常数个变量,因此空间复杂度为
�
(
1
)
O(1)。
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int escapedPrisoners = (int) Math.floor(Math.sqrt(n));
System.out.println(escapedPrisoners);
}
}
}
C++ 代码
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int escapedPrisoners = floor(sqrt(n));
cout << escapedPrisoners << endl;
}
return 0;
}
LaoFang = int(input('输入牢房数量:')) # 牢房数量
list1 = ['OFF'] * LaoFang # 建立列表标记各个牢房的开关状态
count = 0 # 重复了几轮?
while count <= LaoFang:
count += 1
for i in range(0,LaoFang,count):
if list1[i] == 'OFF':
list1[i] = 'ON'
else:
list1[i] = 'OFF'
print(list1.count('ON'))
def count_escaped_prisoners(n):
if n <= 0:
return 0
count = 0
for i in range(1, n+1):
# 如果牢房编号为偶数,则囚犯越狱
if i % 2 == 0:
count += 1
return count
if name == 'main':
t = int(input())
for _ in range(t):
n = int(input())
escaped_prisoners = count_escaped_prisoners(n)
print(escaped_prisoners)
```python
#include<stdio.h>
//牢房指针
char*laos;
//牢房数
int N;
//初始化牢房
void getlao(int n){
laos=(char*)malloc(n);
N=n;
for(int i=0;i<n;i++){
laos[i]=0;
}
}
//玩第n轮
void play(int n){
for(int i=n;i<N;i+=n+1){
if(laos[i]){
laos[i]=0;
}
else{
laos[i]=1;
}
}
}
//统计逃跑
int tj(){
int n=0;
for(int i=0;i<N;i++){
n+=laos[i];
}
return n;
}
//play!!!
int main(){
int num,lun;
printf("输入牢房数量:");
scanf("%d,",&num);
printf("输入轮数:");
scanf("%d,",&lun);
getlao(num);
for(int i=0;i<lun;i++){
play(i);
}
printf("逃跑=%d\n",tj());
}
def count_escaped_prisoners(n):
if n <= 0:
return 0
count = 0
for i in range(1, n+1):
# 如果牢房编号为偶数,则囚犯越狱
if i % 2 == 0:
count += 1
return count
if __name__ == '__main__':
t = int(input())
for _ in range(t):
n = int(input())
escaped_prisoners = count_escaped_prisoners(n)
print(escaped_prisoners)
```python
```
import java.util.Scanner;
public class demo {
public static int countEscapedPrisoners(int n) {
boolean[] cells = new boolean[n]; // 创建一个大小为 n 的布尔数组,表示牢房的状态,默认为 false(锁着的)
for (int round = 1; round <= n; round++) {
for (int i = round - 1; i < n; i += round) {//根据轮数叠加解锁间隔
cells[i] = !cells[i]; // 切换牢房的状态(锁上或解锁)
}
}
int escapedCount = 0;
for (boolean cell : cells) {
if (!cell) {
escapedCount++; // 统计解锁的牢房数量
}
}
return escapedCount;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入牢房数量:");
int n = sc.nextInt(); // 牢房数量
int escaped = countEscapedPrisoners(n);
System.out.println("有 " + escaped + " 名囚犯成功越狱。");
}
}
t = int(input('输入包含一个正整数t(表示有t行数据): '))
result = []
for item in range(t):
number = int(input('请输入轮数n,介于5和100之间(含5和100)的整数:'))
# 默认所有牢房都是关闭状态
current_status = { k:'closed' for k in range(1,number+1) }
# 游戏进行的次数和牢房数量相同
lun = number
for l in range(1,lun+1):
for s in range(l,number+1,l):
if current_status.get(s) == 'closed':
current_status[s] = 'open'
else:
current_status[s] = 'closed'
#把状态是open的保存下来
result.append( len( [ i for i in current_status.values() if i == 'open' ] ) )
for i in result:
print('牢房中越狱的囚犯人数:',i)