acm The Drunk Jailer
来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/11/09 05:55:04
acm The Drunk Jailer
题目·
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.
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
The first line of input contains a single positive integer. This is the
number of lines that follow. Each of the following lines contains a single
integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input
2
5
100
Sample Output
2
10
我的答案
#include // 0 lock,1 unlock
#include
int main()
{
int i,j,n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
int count=0;
int a[100]={0};
for(i=1;i
题目·
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.
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
The first line of input contains a single positive integer. This is the
number of lines that follow. Each of the following lines contains a single
integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input
2
5
100
Sample Output
2
10
我的答案
#include // 0 lock,1 unlock
#include
int main()
{
int i,j,n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
int count=0;
int a[100]={0};
for(i=1;i
1.数组应该声明至少a[101]
2.if(a[j]==0) a[j]=1;
if(a[j]==1) a[j]=0;
不对,这么做没意义,永远会让数组的值为0.把if(a[j]==1)改为else
改正后是没问题的,但是没必要这么麻烦.
解题思路:编号为1的门只为1的倍数,所以只执行了一次操作,所以其最后的状态是开,编号为2的门只为1和2的倍数,执行了两次操作,其最后的状态是关,编号为3的门为1和3的倍数,执行了三次操作,其最后的状态为关,编号为4的门为1、2、4的倍数,执行了三次操作,其最后的状态为开.以此类推,如果编号为一个数的平方数,则其执行操作的次数为奇数次,其最后的状态为开.于是只要求出小于这个数的正整数中有几个数是平方数即可.
代码如下:
#include
#include
int main(void){int n,a,on;
scanf("%d",&n);
while(n--){
scanf("%d",&a);
on=(int)sqrt(a);
printf("%d\n",on);
}
return0;
}
2.if(a[j]==0) a[j]=1;
if(a[j]==1) a[j]=0;
不对,这么做没意义,永远会让数组的值为0.把if(a[j]==1)改为else
改正后是没问题的,但是没必要这么麻烦.
解题思路:编号为1的门只为1的倍数,所以只执行了一次操作,所以其最后的状态是开,编号为2的门只为1和2的倍数,执行了两次操作,其最后的状态是关,编号为3的门为1和3的倍数,执行了三次操作,其最后的状态为关,编号为4的门为1、2、4的倍数,执行了三次操作,其最后的状态为开.以此类推,如果编号为一个数的平方数,则其执行操作的次数为奇数次,其最后的状态为开.于是只要求出小于这个数的正整数中有几个数是平方数即可.
代码如下:
#include
#include
int main(void){int n,a,on;
scanf("%d",&n);
while(n--){
scanf("%d",&a);
on=(int)sqrt(a);
printf("%d\n",on);
}
return0;
}
acm The Drunk Jailer
浙大1350The Drunk Jailer 问题能转化成n以内多少个平方数?为啥还是WA
to the front entranceaid the jailer
nd the soldiers wentaid the jailer
opening in the cell dooraid the jailer
turning to the little gratedaid the jailer
She carried herself very straightaid the jailer
were preventing her from doingaid the jailer
expanding her full bosomaid the jailer
drunk
The jailer was about to lockthe middle of the corridor
ut the jailer closed the doorwhere they were followed