free pascal 编译问题
Freepascal编译问题问题如下狱警的游戏jailer.pas在一个牢狱里,关了N个犯人,当然每个犯人都有一个手铐[不然他就会……].一天晚上,狱警觉得不好玩想来玩一...
Free pascal编译问题
问题如下
狱警的游戏 jailer.pas
在一个牢狱里,关了N个犯人,当然每个犯人都有一个手铐[不然他就会……].一天晚上,狱警觉得不好玩想来玩一个游戏,第一轮他将所有的犯人的手铐都打开,在第二轮将所有犯人号码为2的倍数的手铐又扣上,在第三轮将所有犯人号码为3的倍数的手铐做一次反处理,即如果他的手铐是打开的那么就扣上,如果是扣上的就把它又找开。如此这样进行下去。直到第N轮,游戏结束之后他长长的喝了一只威士忌后扬长而去,当然这时那些手铐被打开的犯人自然可以轻松的夺路而逃了。现在的问题就是:给定N的值,请你确定一下最后有多少个犯人能逃出牢狱。
输入:第一行给出一个数字D〈200,表示有D个测试数据,在它的下面有D行,每一行一个数据,代表犯人的总数。输出:每行输出一个数(〈=100),表示有多少犯人能逃走。
样例输入:
Sample Input jailer.in
2
5
100
Sample Output jailer.out
2
10
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
我的程序是这么编的
program jailer;
var a,b,n,i,j,x,y:longint;
f:array[1..101] of boolean;
begin{1}
assign(input,'jailer.in');reset(input);
assign(output,'jailer.out');rewrite(output);
readln(n);
fillchar(f,sizeof(f),false);
for i:=1 to n do;
begin{3}
readln(y);
begin
for j:=1 to y do
for x:=1 to y do
if (x mod j=0) then
begin
if f[x] then begin f[x]:=true; end;
if not(f[x]) then begin f[x]:=true; end;
end;
end;
begin{7}
b:=0;
for a:=1 to x do
begin
if f[x] then
b:=b+1;
end;
writeln(b);
end;
end;
close(input);close(output);
end.{1} 展开
问题如下
狱警的游戏 jailer.pas
在一个牢狱里,关了N个犯人,当然每个犯人都有一个手铐[不然他就会……].一天晚上,狱警觉得不好玩想来玩一个游戏,第一轮他将所有的犯人的手铐都打开,在第二轮将所有犯人号码为2的倍数的手铐又扣上,在第三轮将所有犯人号码为3的倍数的手铐做一次反处理,即如果他的手铐是打开的那么就扣上,如果是扣上的就把它又找开。如此这样进行下去。直到第N轮,游戏结束之后他长长的喝了一只威士忌后扬长而去,当然这时那些手铐被打开的犯人自然可以轻松的夺路而逃了。现在的问题就是:给定N的值,请你确定一下最后有多少个犯人能逃出牢狱。
输入:第一行给出一个数字D〈200,表示有D个测试数据,在它的下面有D行,每一行一个数据,代表犯人的总数。输出:每行输出一个数(〈=100),表示有多少犯人能逃走。
样例输入:
Sample Input jailer.in
2
5
100
Sample Output jailer.out
2
10
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
我的程序是这么编的
program jailer;
var a,b,n,i,j,x,y:longint;
f:array[1..101] of boolean;
begin{1}
assign(input,'jailer.in');reset(input);
assign(output,'jailer.out');rewrite(output);
readln(n);
fillchar(f,sizeof(f),false);
for i:=1 to n do;
begin{3}
readln(y);
begin
for j:=1 to y do
for x:=1 to y do
if (x mod j=0) then
begin
if f[x] then begin f[x]:=true; end;
if not(f[x]) then begin f[x]:=true; end;
end;
end;
begin{7}
b:=0;
for a:=1 to x do
begin
if f[x] then
b:=b+1;
end;
writeln(b);
end;
end;
close(input);close(output);
end.{1} 展开
1个回答
展开全部
模拟,方法是对的,但编错的地方太多了。。
出错的地方及解决方案:
1、大循环 for i:=1 to n do 后面加了个分号,以至于循环空做,主程序只做了一遍,把分号去掉
2、fillchar 放在循环外,改为放到循环内,每遍都要还原
3、将手铐已打开(true)的人变为未打开(false)时打错了,以及变化只有一次,所以要改分号为 else
4、统计时循环应为 for a:=1 to y do
弄清楚程序的结构,以及每个变量是储存什么的,否则看上去很乱,自己也容易弄混
出错的地方及解决方案:
1、大循环 for i:=1 to n do 后面加了个分号,以至于循环空做,主程序只做了一遍,把分号去掉
2、fillchar 放在循环外,改为放到循环内,每遍都要还原
3、将手铐已打开(true)的人变为未打开(false)时打错了,以及变化只有一次,所以要改分号为 else
4、统计时循环应为 for a:=1 to y do
弄清楚程序的结构,以及每个变量是储存什么的,否则看上去很乱,自己也容易弄混
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询